Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP Wish List | Report Bug | About Me ]

2.5     Running PHP scripts

This is NOT the latest copy of this book; click here for the latest version.

You can execute your scripts in one of two ways: through a web server where the output is a web browser, or through the command line where the output is the command line itself. Of the two, the former is much more popular, but the latter is steadily growing in popularity.

We cover both methods of script execution in this book, with the bias towards web server usage. Most scripts will work smoothly in either environment, with the exception of scripts covered in the chapter "Alternative PHP uses", which require specific environments. You're encouraged to try using both when reading this book - it will give you more familiarity with how PHP works, and will also give you good experience using the CLI SAPI to debug your scripts.

The primary difference between outputting text to the command line and to a web browser is the format of new lines - through the CLI you need to use \n for a new line, whereas for web browsers you need to use the HTML line break, <BR />. If you want to take a script designed for CLI and make it work through the web, swap \n for <BR />, and vice versa for converting web scripts to command line scripts.

Running scripts through your web server is as simple as putting the PHP script into your web server's public directory, then navigating to the appropriate URL with your browser. Hopefully you will already have followed the official PHP installation instructions for your platform!

Running scripts through the command line is done using the CLI SAPI, which, if you are using Windows, can be found in the CLI directory of your PHP installation. That is, if you have installed PHP into c:\php, the CLI SAPI will be in c:\php\cli\php.exe. If you are using Unix, the availability of the CLI SAPI is down to how you installed PHP - make sure and issue the command "make install-cli" after everything else in order to install it.

Later on we'll look at more detailed information on how using the CLI SAPI is different from normal PHP scripting, so don't rush off and try it out just yet!

If you are not sure about whether PHP is set up on your machine correctly, the best way forward is to run the following script:

<?php
    phpinfo
();
?>

That calls a special function, phpinfo(), which outputs all the information PHP currently has - how it was configured, what server it is running on, what modules are available, and more. It is very handy to keep around when you are developing, as it will answer most questions you have about configuration.





<< 2.4 PEAR   2.6 How PHP is written >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

sdfdfsdfsdfsdfsdfsdfsd

A PHP User - 06 Sep 2008

I wanted to use PHP with a MySQL database and I was struggling to install them separately, when I discovered XAMPP from a link in the PHP site. It installed PHP, MySQL. An apache server and a few other useful bits in about 10 minutes. Then it provided a control box on my PC desktop which allowed me to start the webserver and database easily. Testing out the examples in this book were then so easy, just write a script, save to the XAMPP htdocs directory and then point your browser at 127.0.0.1/{filename}.

Canadian PHP User - 06 Sep 2008

I have no CLI directory under my PHP tree. I used EasyPHP for installation ... can I download and install the PHP CLI without ruining EasyPHP's configurations?

Canadian PHP User - 06 Sep 2008

I have no CLI directory under my PHP tree. I used EasyPHP for installation ... can I download and install the PHP CLI without ruining EasyPHP's configurations?

Canadian PHP User - 06 Sep 2008

I have no CLI directory under my PHP tree. I used EasyPHP for installation ... can I download and install the PHP CLI without ruining EasyPHP's configurations?

A PHP User - 06 Sep 2008

You REALLY do need to have a whole chapter devoted to installing the required software to use and view PHP. I have no idea what install, configure, etc. The book is useless otherwise. This should be the FIRST chapter in the book...without it, you can read all day and never see what you are doing. Inexcusable. Glad I tried the free version before buying a book.

A PHP User - 06 Sep 2008

If you're a true newbie, and are completely lost as to how to start installing what is required to actually write and view PHP in your web browser, I suggest the following website:

http://mpcon.org/apacheguide/apache.php

That is about as hold-your-hand as you can get in how to install apache. Once apache is installed, follow his guides on installing PHP and MYSQL as well, both of which are also geared towards the complete newbie.

I myself am basically new to PHP, and one thing I had to hunt forever to find was how to end a line and start a new line if you were not outputting something within double quotes. ie:

print "blahblah";
or
print $blah;

There are about a gazillion code examples for the first one:
print "blahblah<br />";

but strangely enough I've yet to actually find an official way to do it for the second one.

Currently I am doing:
print $blah;
echo '<br />';

but what I was wondering is if there is a simpler, shorter way of achieving the linebreak.

A PHP User - 06 Sep 2008

If you're a true newbie, and are completely lost as to how to start installing what is required to actually write and view PHP in your web browser, I suggest the following website:

http://mpcon.org/apacheguide/apache.php

That is about as hold-your-hand as you can get in how to install apache. Once apache is installed, follow his guides on installing PHP and MYSQL as well, both of which are also geared towards the complete newbie.

I myself am basically new to PHP, and one thing I had to hunt forever to find was how to end a line and start a new line if you were not outputting something within double quotes. ie:

print "blahblah";
or
print $blah;

There are about a gazillion code examples for the first one:
print "blahblah<br />";

but strangely enough I've yet to actually find an official way to do it for the second one.

Currently I am doing:
print $blah;
echo '<br />';

but what I was wondering is if there is a simpler, shorter way of achieving the linebreak.

A PHP User - 06 Sep 2008

You don't even mention anything about installing PHP. Somehow I'm reading info about PHP, then suddenly I'm supposed to have installed it already? What the hell? This is a terribly written "book."

A PHP User - 06 Sep 2008

Im lost...completely, i cant even find where you open the bloody php program itself, let alone what you do, this is startign to seem hopeless...

A mexican new PHP User - 06 Sep 2008

Hi. Finally before 3 sessions of being studying how to use the code mentioned in this page, I made it.

I used the installer EasyPHP, and it made a folder called www , in that folder I saved the archive with the code with the name info.php, then I inserted the next URL:

http://127.0.0.1/info.php

it works!!

But I think I spent much time understanding where do I had to save the file.

Re: A PHP Newbie - 06 Sep 2008

--------------------------------
QUOTE:
1. Am I supposed to create a text file with
<?php
phpinfo();
?>
in it? If so, what should I name it? Should the file extension be php, html, txt or nonexistent?
2. Is that a tab or spaces in front of phpinfo?
---------------------------------

1. 'Running a script' means, at least in this case, putting the code into a .php file and executing it in your browser - opening the page, basically. For this example no other code is needed at all. The file extension must be .php unless stated otherwise so your browser knows to parse the information as a .php script.

2. The gap in front is irrelevant, it is just used to space out code so it is easy to read.

A PHP User - 06 Sep 2008

In your examples where you use <BR />, this is incorrect syntax; <br /> is only supported in XHTML, and requires lowercase tag and attributes. Uppercase tags and attribs are only allowed in HTML, in which case the self closing element /> is not needed, or supported. For more information, check out www.w3c.org for the HTML and XHTML RFCs.

Another PHP Newbie - 06 Sep 2008

The text, heretofore easy to follow for this newbie of newbies, suddenly assumes I know too much. I would like to try the web browser option. Unanswered questions include:

1. Am I supposed to create a text file with
<?php
phpinfo();
?>
in it? If so, what should I name it? Should the file extension be php, html, txt or nonexistent?
2. Is that a tab or spaces in front of phpinfo?

A PHP User - 06 Sep 2008

I wanted to use PHP with a MySQL database and I was struggling to install them separately, when I discovered XAMPP from a link in the PHP site. It installed PHP, MySQL. An apache server and a few other useful bits in about 10 minutes. Then it provided a control box on my PC desktop which allowed me to start the webserver and database easily. Testing out the examples in this book were then so easy, just write a script, save to the XAMPP htdocs directory and then point your browser at 127.0.0.1/{filename}.

A PHP User - 06 Sep 2008

There is no CLI directory in my PHP directory. You need to list off step by step how to install such a difficult process. This is madness.

A PHP Newbie - 06 Sep 2008

Yeah, how in the world do you install most of the stuff you're talking about? Stupid open source crap! Why can't they make installers for everything?

mosweuk@bbl.bw - 06 Sep 2008

I am not sure of the installation you are using, check paragraph 5, in php 5.0.3 (manual install) the CLI SAPI executale is in the install diriectory under windows i.e. c:\php\php.exe



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow