PHP SAPI on Mac OS X Leopard

Discussion in 'Web Design and Development' started by dataset, Dec 31, 2007.

  1. macrumors member

    Joined:
    Jun 27, 2007
    Location:
    La Mirada, California, United States Of America
    #1
    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!
     
  2. macrumors newbie

    Joined:
    Nov 8, 2007
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Jun 27, 2007
    Location:
    La Mirada, California, United States Of America
    #3
    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?
     
  4. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #4
    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.
     
  5. thread starter macrumors member

    Joined:
    Jun 27, 2007
    Location:
    La Mirada, California, United States Of America
    #5
    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!
     
  6. macrumors newbie

    Joined:
    Nov 8, 2007
    #6
    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.
     
  7. Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #7
    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.
     
  8. macrumors newbie

    Joined:
    Nov 8, 2007
    #8
    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
     
  9. Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #9
    So you do... that's weird - I've obviously never used it with variables! Thanks for the correction.
     
  10. macrumors newbie

    Joined:
    Oct 14, 2008

Share This Page