Python (CPython and Jython at least, possibly all Pythons) have a function called compile that can be used to compile a file into a string into or a bytecode object.
# foo.py
a = compile("""
print(47)
""", filename="testfile", mode="exec")
exec(a)
Running foo.py produces:
$ python foo.py
47
Does PHP (Zend specifically, any version) expose its bytecode compiler to user code in a similar way? More specifically, can you dynamically compile strings without executing them like eval does.
i don't know but what you show me it's very familiar to this alternative you can improvise:
foo.php
<?php
print(47);
?>
index.php
<?php
include('foo.php');
?>
eg: so in php cli under windows cmd:
in any folder you have index.php and foo.php with xampp from apachefriends.org installed in c:\xampp create runme.bat
#echo off
set parameter=%1
set PHP_BIN="c:\xampp\php\php.exe"
%PHP_BIN% %parameter%
so from cmd you can call very easy cli interpretation of index.php :
runme index.php
also runme foo.php had the same results ,the only reason i put there index.php is to use it as a "proxy" between a somehow array list i store in and to include not only foo.php but manny others and in this way could became a kind of "controller" with not much modifies(if the question is pointing to sort of code executions in any order ordered by a kind of controller)
47
If you want to know whether PHP has the compile builtin function to compile the PHP code, there is no. PHP is a interpreted language, PHP code need interpreter to run it, and the interpreter has the executing environment for it.
But if you want to realize your requirement, here is some way to achieve it:
PHC
phc is an open source compiler for PHP with support for plugins. In addition, it can be used to pretty-print or obfuscate PHP code, as a framework for developing applications that process PHP scripts, or to convert PHP into XML and back, enabling processing of PHP scripts using XML tools.
bcompiler
To encode entire script in a proprietary PHP application
To encode some classes and/or functions in a proprietary PHP application
To enable the production of php-gtk applications that could be used on client desktops, without the need for a php.exe.
To do the feasibility study for a PHP to C converter
3.Project Zero
It supports the PHP compile.
Related
I would like to be able to debug the PHP source while running specific scripts in order to see what PHP does with the script internally and thus learn how PHP works "under the hood". How can I configure this workflow?
As the title indicates, I have MATLAB code for isolated spoken words recognition, and I want to be able to integrate this project with another one made with PHP for some purpose.
I have not used to deal with such problem before. In other words, it's the first time for me when I need to integrate PHP and MATLAB, so I really don't know where to start and how.
I have read a couple of articles, but I couldn't make it valid.
I have PHP 5.4.9, MATLAB R2012A and Windows 7.
The MATLAB project files can be seen on GitHub.
You have a few options here:
If MATLAB installed on the server where the PHP application would be deployed (not your current development environment), you can invoke it directly just like any other program (matlab -r "...") using whatever is the equivalent of EXECUTE command in PHP. Here are some resources (make sure to also checkout the linked questions as well):
How to call MATLAB from command-line and print to stdout before exiting
Running a cmd file without GUI popping up
Pass Parameters _ Shell Script - Octave Script
Others have commented on how to pass input/output between PHP and your MATLAB script. For example, you could design your MATLAB function to receive the path of WAV file as input, process it and save any resulting image to disk:
function myFunc(filename)
[y,Fs] = audioread(filename);
img = my_process_func(y, FS);
imwrite(img, 'out.png');
end
Which is invoked from PHP as:
% Of course you have to make sure "myFunc" is available on the MATLAB path.
% Think: "addpath(..)" or just "cd(..)" into the directory first
matlab -wait -nodisplay -r "myFunc('audio.wav'); quit;"
You could then read the output image in the PHP application.
If not, what deployment-related toolboxes do you have available? MATLAB Compiler and related toolboxes like MATLAB Builder NE and MATLAB Builder JA.
Those will compile your program into an executable/.NET Assembly/JAR file respectively, and all of them require the freely available MCR Runtime to be installed. In other words, the executables do not need to have a full MATLAB installation on the target machine, only the MCR runtime.
You would run the executable in the same manner as before.
Another product is the MATLAB Coder, which converts your MATLAB code into C++ program. When compiled, it can run without any external requirement.
A new product by MathWorks is MATLAB Production Server. Personally I know nothing about it :)
Yet another option is to use TCP/IP to communicate between PHP and MATLAB. A server would be run on the MATLAB side, using socket programming written as C MEX-file or a Java class. See:
MATLAB Mex Socket Wrapper Library
Writing Java's pw.println(), etc. in MATLAB
The client being your PHP application. The idea is to have MATLAB listening for connections, reading whatever input is given by a client, eval it, and return the result. This is more involved than the other options, as you have to deal with serialization and other things like concurrency. The advantage is that MATLAB can be run on a separate server, even on multiple servers on the cloud (see this post).
So first, decide what approach best suits your project, then it would be easier to answer specific questions... Just always consult the documentation first, MATLAB toolboxes are very well documented and usually include many examples. Here are a couple more resources specific to MATLAB Compiler products family:
Webinar: Application Deployment with MATLAB
PDF File: MATLAB Application Deployment - Web Example Guide
Note that they concentrate on ASP.NET and Java JSP/servlet applications. In your case, the PHP application would communicate with a middle tier running a web service built using one of the above two options (or simply design a CGI-like site running plain executables built using the MATLAB Compiler as explained earlier).
To help the OP with running system commands from a PHP webpage, my post here is relevant (copied below).
We do exactly this all the time. I call them voodoo pages. Here's some working code:
<?php
$command="uptime"; $output; $retval; $errors="";
exec ($command, &$output, &$retval);
echo $output[0] . "\n";
unset($output);
?>
And the output to the webpage served:
13:40:19 up 22 days, 23:14, 0 users, load average: 0.04, 0.02, 0.00
And the additional note I added in the comments below:
Relative vs absolute paths may be a pain... $command might need to be /usr/bin/uptime or another could be /usr/bin/ls /home/chris/ftp. Normally, scripts' working directory is where they live in the file system. MATLAB is a windows program, yes? My experience is you will need absolute paths for the program and any files passed as arguments, example: $command="c:\\matlab\\matlab.exe c:\\www\\somefile.wav" And then single quotes required for silly NTFS names, TAB command line completion works well for examples. Or use proper 8.3 name with the ~ in it.
My answer would be in two parts:
How do I run a MATLAB script from the terminal?
I will give some example about running a MATLAB script from the terminal:
matlab -nojvm -nodesktop -r "run <the-script>.m"
matlab -nojvm -nodesktop -r "<the-script>"
matlab -nojvm -nodesktop -r "run <the/path>/<the-script>.m"
matlab in Windows must be in your environment path. How-to.
If you need to compile your script to Java:
java -jar yourjarfile.jar
How do I execute the terminal command from PHP?
I think the previous answers are good, and there isn't any need to repeat them.
More notes:
Watch for your security. You might be XSS'ed easily.
Abstract your code and improve it to save parameters and output to the database. Run your code in
Parallels or queue manager. You might create a REST service.
Unit test.
Use Linux. It's much more powerful.
One quick hack would be to compile your MATLAB code into an executable file then use PHP's shell_exec().
The difficult part would be adapting your MATLAB code (sorry, I didn't read it) in such a way that:
It will receive its input in command-line-interface style (as char strings);
It will output its results as text to standard output (file id #1 in MATLAB).
Then all it takes is to parse the MATLAB output back into PHP...
I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?
EDIT
Or is there a tool that can debug into the c code that's actually executed?
Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.
The really core stuff is in ext/standard.
Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.
About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.
For more informations, you can take a look at :
Building PHP for extension development
Generating a gdb backtrace
I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.
As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.
I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.
I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?
http://www.adp-gmbh.ch/win/misc/webserver.html
I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!
The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.
There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.
So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.
How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe
Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.
PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.
I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.
Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html
This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.
You need execute the PHP page to serve the page it generates.
The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.
Once you have CGI support you can just use any executable, including PHP, to generate webpages.
I'm not much of a programmer, PHP is where I'm comfortable. And sometimes I find that I need to do things, such as arrange files or rename files on a mass scale on my computer. And I think I could do this with PHP but I can't of course.
So I was curious, is there a way I could run PHP files as kind of exe files.
EDIT: Fairly important point, using Windows.
just use php.exe (put it in your path) and the name of the php file you want to execute
You should have a look at php gtk
It's not as bad as you put it. PHP may be a very good tool for string related stuff like parsing, renaming etc. Especially if you know PHP.
To use php as script you should add #!/path/to/php as first line and set execution permissions on unixoid systems. In windows you can simply assign the php file ending with your php cli exe so you can click on them or use the script with the "start" command in the windows shell. But make sure that you write your scripts in a way that it is sensible to the current working directory. It may be different to what you might expect sometimes.
to be able to execute php files with double click, just like normal programs, go to the command line, then type
ftype php_script "C:\path\to\php.exe" "%1"
assoc .php=php_script
Check out WinBinder
Sure, just add #!/path/to/php to the top of the file, add the code in tags, and run it as a shell script.
Works fine - the php binary you use is either the cgi one or the purpose built CLI version.
http://www.php-cli.com/
http://php.net/manual/en/features.commandline.php
it would appear so, yes.
Download Wamp Server, install it. Once thats done, add the path to the php.exe to your PATH settings. You can do this by going to control panel->system->change settings->advanced->environment variables. Edit the PATH, add a ';' to the end of the line and then past the path to the php.exe. This is on Vista, it might be different on XP or Windows 7.
My path looks like this after: C:\Sun\SDK\jdk\bin;C:\wamp\bin\php\php5.3.0
Once thats done, you'll be able to execute a php file from the command line. You could create shortcuts too.
C:\Users\Garth Michel>php test.php
This is a test
C:\Users\Garth Michel>
I used php for years as a scripting language before I even bothered to use it as a web programming language.
maybe php is not the right tool to doing this and it's about time to learn another language...use this chance to expand your programming horizion
See the .reg file in this gist, it makes it possible to use .php files exactly like .bat files, e.g. my_script.php foo bar baz
Don't forget to edit paths to suit your setup.
http://www.appcelerator.com/products/download/ Still use html & css as a desktop app and now has support for php.
PHP based web apps like Wordpress and Mediawiki I think uses php to setup and configure itself. Just give IIS proper read/write rights and you can make a simple web app that does massive renaming, etc.. PHP doesn't have to always be used for writing out html.
See ExeOutput for PHP. It has excellent customization and MAGNIFICENT features. Yet it is not an IDE or something. It compiles your PHP+HTML into fully-fledged EXEs.