Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
PHP 5 is included in Leopard, along with the Apache 2 SAPI (in the form of an Apache module). You can enable the PHP module in the default Apache 2 installation by editing the httpd.conf file:

In the terminal:
sudo pico /etc/Apache2/httpd.conf

If you scroll down, you should see this line near the bottom of the list of modules to load:

#LoadModule php5_module libexec/apache2/libphp5.so

Remove the (#) from the beginning of the line, and ctrl + x to save and quit. Then go into Sharing in System Preferences, and uncheck, then recheck "Web Sharing". You should be good to go.
 
Thanks, but I had already done that, so do I just type php in the command line to bring up the PHP command line interface?
I tried typing PHP in the terminal and it just sits there and doesn't do anything or give me another prompt. Can someone help me here?
 
I tried typing PHP in the terminal and it just sits there and doesn't do anything or give me another prompt.

What did you expect? That what PHP does. It is reading from "standard input".

Most people do not type in PHP from the command line. They use an editor to make a file and then give the file to PHP. Kind of the same with writing a shell script, C or Perl program.
 
What did you expect? That what PHP does. It is reading from "standard input".

Most people do not type in PHP from the command line. They use an editor to make a file and then give the file to PHP. Kind of the same with writing a shell script, C or Perl program.

I had read somewhere that you could use PHP from command line to test functions and other things by typing them into there. The article must have been wrong. Thanks anyways though!
 
Nope, but as ChrisA said, you can give PHP a script to execute in the terminal:

pico ~/test.php
<?php $a="foo"; $b="bar"; echo $a.$b; ?>
ctrl + x to save and exit pico
php ~/test.php

will result in "foobar" in the terminal. So put your functions into a php file to test them in the terminal.
 
For very simple PHP code (and I do mean very simple!) it is possible to run it from the command line without saving it to a file first, using: (nicking bhess' example)

Code:
echo "<?php $a='foo'; $b='bar'; echo $a.$b; ?>" | php

Need to watch that you escape (add backslashes before) any quotes, or change them to single quotes as I did above. Once you get beyond single-line code snippets it just gets messy so its usefulness is a bit limited, but I find it handy in some situations.
 
If you wrap in double quotes, you will have to escape out the $ symbol as well. Better to wrap the whole thing in single quotes, and then single quotes are the only characters you'll have to escape out within the PHP snippet.

So the example given above should actually be:
Code:
echo '<?php $a="foo"; $b="bar"; echo $a.$b; ?>' | php
 
If you wrap in double quotes, you will have to escape out the $ symbol as well. Better to wrap the whole thing in single quotes, and then single quotes are the only characters you'll have to escape out within the PHP snippet.

So the example given above should actually be:
Code:
echo '<?php $a="foo"; $b="bar"; echo $a.$b; ?>' | php

So you do... that's weird - I've obviously never used it with variables! Thanks for the correction.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.