PDA

View Full Version : PHP SAPI on Mac OS X Leopard




dataset
Dec 31, 2007, 06:12 PM
Hello everyone!
I was wondering if the PHP SAPI is enabled on Mac OS X Leopard and how to access it if is.
If its not is there anyway to enable it without having to compile PHP for myself?
Thanks!



bhess
Jan 1, 2008, 06:59 PM
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.

dataset
Jan 1, 2008, 11:10 PM
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?

ChrisA
Jan 2, 2008, 01:09 PM
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.

dataset
Jan 2, 2008, 02:02 PM
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!

bhess
Jan 3, 2008, 06:43 AM
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.

Knox
Jan 3, 2008, 07:34 AM
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)

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.

bhess
Jan 3, 2008, 07:24 PM
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:
echo '<?php $a="foo"; $b="bar"; echo $a.$b; ?>' | php

Knox
Jan 4, 2008, 05:23 AM
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:
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.

baileylo
Oct 14, 2008, 05:33 AM
http://hudzilla.org/phpwiki/index.php?title=Your_first_CLI_script