Could anyone please tell me how to run a php file locally on my system.
Currently I am using a server to run files.
I know both php & Apache to be installed.
I need to see out put of this program, for example:
<?php
$a=5;
$b=10;
$c=$a+$b;
print $c;
?>
Can you please tell how I can run these files of if I need anything more.
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
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.
In short:
Install WAMP
Put this file to C:\wamp\www\ProjectName\filename.php
Go to browser: http://localhost/ProjectName/filename.php
I just put the content in the question in a file called test.php and ran php test.php.
(In the folder where the test.php is.)
$ php foo.php
15
If you have apache running, put your file in server folder for html files and then call it from web-browser (Like http://localhost/myfile.php ).
Running PHP script directly in browser:
Here are all steps (in short) to run PHP program in XAMPP
Step 1: First of all, open the Apache Friends website, The download and install XAMPP for Windows.
Step 2: Open the XAMPP Program Control Panel and start “Apache Web Server”.
(NB: If your PHP scripts need MySQL database to work, Start “MySQL” service as well)
Step 3: Create a new folder inside “htdocs” folder. For example: “php-project”
Path looks like: C:/xampp/htdocs/php-project/
Step 4: Open any text editor and create a new file and write a simple PHP program then Save it with .php extension as “example.php”.
Path looks like: C:/xampp/htdocs/php-project/example.php
<?php
echo “Hello Level 5 Developers”;
?>
Step 5: Go to the browser and type “localhost/php-project/example.php” in the address bar and press enter then it gets executed.
Step 6: After that, you can see the output of your first PHP program like this:
Hello Level 5 Developers
3 easy steps to run your PHP program is:
The easiest way is to install MAMP!
Do a 2-minute setup of MAMP.
Open the localhost server in your browser at the created port to see your program up and runing!
Related
I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.
Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php
I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.
To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php
You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.
Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.
Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.
Where can I write the following command
i am following the developer tutorial for simple hello world script here
google_appengine/dev_appserver.py --php_executable_path=pathto-php-cgi path-to-your-app
When I run application in google app launcher I get a error message . I have WAMP install and it is running fine.
The path specified with the --php_exectuable_path flag () does not exist.
Php path - c:\php\php-cgi.exe
I also met this problem, and it has been solved.
for example,
if your PHP folder is E:/sam/php/
if your PHP site folder is F:/mysite/facebook/
if your dev_appserver.py is under E:/whatever/
I'm not sure how to change googleapplauncher.exe configurations, instead, you have to use cmd instead. I think this exe has config files but I did not find it. Open the command window and go to folder E:/whatever/ to run "python" command under this folder.
Type in:
python dev_appserver.py --php_executable_path=E:/sam/php/php-cgi.exe F:/mysite/facebook/
NOTE: REMEMBER to type "php-cgi.exe"
use slash
I dont know which is the name of the file in Windows, but i just have that problem, the full path from / for your php-cgi54.exe including the fullname of the file and then the full path from / of your folder.
I also run it like admin so check if your cmd or terminal was open with admin rights.
#Chander if you have wamp running you need to select a different listening port for app engine by typing the command: --port=9999 for example.
Here's what worked for me:
Open your cmd, make sure you're in the same directory as your sdk.
Type: "google_appengine/dev_appserver.py" --port=9999 --php_executable_path="fullpath\php-cgi.exe" "helloworld/"
If you have another local server like xampp or wamp,make sure you use the --port command to select another listening port as shown above.
Press enter and you should have your helloworld script working.
I want to run a .php file in windows cmd.
I followed this (suggestion)
php is not recognized as an internal command (in Windows)
But it did not work.
I tried this command prompt: C:\Windows\system32> cd \myfolder
And within myfolder I did as follows:
C:..\myfolder> php file.php
but I get an error "php is not recognized as internal or external"
But when I try without php command like this; C:..\myfolder> file.php
it just opens in notepad, yet I want to run it in cmd. How can I do it?
If running Windows 10:
Open the start menu
Type path
Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.
you can for example: set your environment variable path with php.exe folder e.g c:\program files\php
create a script file in d:\ with filename as a.php
open cmd: go to d: drive using d: command
type following command
php -f a.php
you will see the output
You should declare Environment Variable for PHP in path, so you could use like this:
C:\Path\to\somewhere>php cli.php
You can do it like this
It seems your question is very much older. But I just saw it.
I searched(not in google) and found My Answer.
So I am writing its solution so that others may get help from it.
Here is my solution.
Unlike the other answers, you don't need to setup environments.
all you need is just to write php index.php if index.php is your file name.
then you will see that, the file compiled and showing it's desired output.
My system is win7. I have installed XAMPP on the system successfully.Besides, I have set the environment variables:
PHP_HOME=C:\xampp\php
Path=%PHP_HOME%
But when i run php code in git bash ,it appears the error:"sh.exe:php:command not found"
How can i solve the problem?
In my case I can solved my problem in this way ...
Go to PHP directory in the XAMPP folder and run the code in this way with the XAMPP server:
$ Php /c/xampp/htdocs/cupon/bin/vendors install
As far as i was trying to execute MSVC compiler from within Java, i got the idea of all that Win7: the shell you are running something has different environment variables that you think you've set.
I recommend you to insall WAMP server - it is totally compatible with Windows while XAMPP seems to be a bit hard way to start with.
And one more hint: try running php / sh from the bin/ PHP directory.
If you're using Windows and getting the "php: command not found" error, it means that the PHP executable is not in your system's PATH environment variable. Here are the steps to fix it:
Find the location of your PHP installation. By default, PHP is installed in the "C:\php" directory. If you've installed PHP in a different location, note down that path.
Add the PHP directory to the system's PATH environment variable:
Open the Start menu and search for "Environment Variables."
Click on "Edit the system environment variables."
Click on the "Environment Variables" button at the bottom of the window.
Under "System Variables," scroll down and find the "Path" variable. Click "Edit."
Click "New" and enter the path to your PHP directory (e.g., "C:\php").
Click "OK" to close all the windows.
Close and reopen your terminal or command prompt to apply the changes.
Verify that PHP is working by running the command "php -v" in your terminal or command prompt. You should see the version of PHP installed on your system.
If PHP is still not working after following these steps, you may need to reinstall PHP or check that the PHP executable file is in the correct directory.
‘php.exe’ is not recognized as an internal or external command,
operable program or batch file.
Why does that error occur even though I've added PHP to my environment variables?
My environment variable PATH is shown below:
C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\Program
Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\PC
...............
Files\QTSystem\;A:\xampp\php\php.exe
I'm using Windows 7.
A:\xampp\php\php.exe
The PATH environment variable must contain paths only, not file names.
Make that
A:\xampp\php
it is very easy to do
You need to also add php.exe
Adding this path:
windows key + pause
click on Advance system settings
click Environment Variable
System variables part -> Path -> Edit and the add
C:\wamp\bin\php\php5.3.8;
close you cmd prompt if its open
It's Done!
I think you need to change it to A:\xampp\php\ and just leave it there, then it will search the directory for the file, also: have you tried calling just php instead of php.exe and check that the directory is correct.
View above screen shot for setup enviroment variable in window 7
Note:- After setup enviroment variable please restart you computer then its will work fine
Shouldn't the last part be: C:\xampp\php\ ? Unless you remapped your drives, A is the floppy disk. :)
Also, you need to specify the folder, not the exe file itself.
My case am Using Bitnami Ruby Stack apache Web server ,
Pressed Windows key + Pause/Break Button
Clicked advanced system settings
Click Environment Variables
Under System variables > edit PATH variable by adding C:\Bitnami\rubystack-1.9.3-24\php
Click Ok to close the Environment Variable window
Close your command prompt if open
*(Proceed to 7. to confirm if php is installed)
Press Windows Key + R button to open your cmd
Type php -v to view your php version
Set PHP as environment variable from System as below to run it from CMD
1)Windows key + Pause
2)Click on Advanced system settings
3)Now click on Environment Variables.. button
4)In system variable section search for Path and edit it as D:\wamp\bin\php\php5.3.13;(If one needs to append it to some already exits value then start path with;)
5)Now open CMD and run command PHP from any path where you don’t have PHP installed , in my case it’s C drive.
6)We can check above set Path from CMD using Path command also.
If you have installed WAMP server, then your path can vary. For example, I installed WAMP and the path to my php.exe turned out to be:
C:\wamp\bin\php\php5.5.12\php.exe
To make it work, I entered the following path in the PATH variable of the System Variables:
C:\wamp\bin\php\php5.5.12
Once done, I restarted command prompt and everything worked fine.
So the path can vary depending upon how you installed PHP. If you would like to install WAMP server and have it install other essential packages like PHPMyAdmin, etc. you may do so from: wampserver.com
In my case, environment path is C:\ProgramData\Drush\;C:\Program Files (x86)\Drush\Php; and it works for me.
If I simply add C:\xampp\php\ dint work for me.
I hope if someone facing this issue can try this suggestion.
This happened to me and I found why.
Xampp can't run properly depending on your UAC settings.
Try to do this:
Log in as administrator (running cmd.exe as administrator isn't enough)
Execute cmd.exe or equivalent
type php -version