php have a easy way to run a light server:
first cd into php file directory, then
php -S 127.0.0.1:8000
then you can run php
Answer from peter zhang on Stack Overflowphp have a easy way to run a light server:
first cd into php file directory, then
php -S 127.0.0.1:8000
then you can run php
You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.
What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).
Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.
Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.
How to install php and run it?
function - How can I execute PHP code from the command line? - Stack Overflow
Run PHP ... More on stackoverflow.com How to make PHP environment setup easier for developers
Execute php file from another php - Stack Overflow
How do I run my PHP file?
How to run PHP files in phpMyAdmin?
How to run a PHP file in localhost XAMPP?
Videos
How do I install php and run it? I've seen people running it on the command prompt on Youtube but I would like a way where I can run it on Notepad++ or Sublime Text (preferably Notepad++).
If you're going to do PHP in the command line, I recommend you install phpsh, a decent PHP shell. It's a lot more fun.
Anyway, the php command offers two switches to execute code from the command line:
-r <code> Run PHP <code> without using script tags <?..?>
-R <code> Run PHP <code> for every input line
You can use php's -r switch as such:
php -r 'echo function_exists("foo") ? "yes" : "no";'
The above PHP command above should output no and returns 0 as you can see:
>>> php -r 'echo function_exists("foo") ? "yes" : "no";'
no
>>> echo $? # print the return value of the previous command
0
Another funny switch is php -a:
-a Run as interactive shell
It's sort of lame compared to phpsh, but if you don't want to install the awesome interactive shell for PHP made by Facebook to get tab completion, history, and so on, then use -a as such:
>>> php -a
Interactive shell
php > echo function_exists("foo") ? "yes" : "no";
no
If it doesn't work on your box like on my boxes (tested on Ubuntu and Arch Linux), then probably your PHP setup is fuzzy or broken. If you run this command:
php -i | grep 'API'
You should see:
Server API => Command Line Interface
If you don't, this means that maybe another command will provides the CLI SAPI. Try php-cli; maybe it's a package or a command available in your OS.
If you do see that your php command uses the CLI (command-line interface) SAPI (Server API), then run php -h | grep code to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup.
Another couple of examples, just to make sure it works on my boxes:
>>> php -r 'echo function_exists("sg_load") ? "yes" : "no";'
no
>>> php -r 'echo function_exists("print_r") ? "yes" : "no";'
yes
Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI. It is likely that several PHP SAPIs use different php.ini files, e.g., /etc/php/cli/php.ini vs. /etc/php/cgi/php.ini vs. /etc/php/apache/php.ini on a Gentoo Linux box. Find out which ini file is used with php -i | grep ini.
Using PHP from the command line
Use " instead of ' on Windows when using the CLI version with -r:
Correct
php -r "echo 1;"
Incorrect
php -r 'echo 1;'
PHP Parse error: syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1
Don't forget the semicolon to close the line (otherwise, the result is "PHP Parse error: syntax error, unexpected end of file, expecting ';' or ',' in Command line code on line 1").
After installing PHP locally and running composer install, is it possible to have a development setup similar to “yarn start:dev” for Node, but in PHP without the use of Nginx and php-fpm?
Usually I would need to have a docker-compose.yml containing a docker image in the project file. The docker image is production ready and ends up being deployed to kubenetes, but sometimes it’s difficult for other developers to run cli/console commands, especially those who come from a Node background (as you’d need to bash/shell into the container).
It would be easier if users could start a PHP development environment and the necessary extensions by running an executable, without the use of a docker container. Or would it be easier to have the production container run without nginx and php-fpm?
Does a solution exist?
It's trying to run it as a shell script, which interprets your <?php token as bash, which is a syntax error. Just use include() or one of its friends:
For example, in a.php put:
<?php
print "one";
include 'b.php';
print "three";
?>
In b.php put:
<?php
print "two";
?>
Prints:
eric@dev ~ $ php a.php
onetwothree
exec is shelling to the operating system, and unless the OS has some special way of knowing how to execute a file, then it's going to default to treating it as a shell script or similar. In this case, it has no idea how to run your php file. If this script absolutely has to be executed from a shell, then either execute php passing the filename as a parameter, e.g
exec ('/usr/local/bin/php -f /opt/lampp/htdocs/.../name.php)') ;
or use the punct at the top of your php script
#!/usr/local/bin/php
<?php ... ?>