Running a PHP Script

Discussion in 'Web Design and Development' started by Ides, Sep 15, 2012.

  1. Ides macrumors member

    Joined:
    Mar 27, 2012
    #1
    So sorry if this question is really obvious but....

    I've done a lot of objective c programming on iphone and mac but I want to learn some other languages, specifically web languages. I already know some basic HTML and I'm following a tutorial on PHP. I'm using Komodo Edit to type up my PHP script. But I save my hello world script as a php file but I don't know how to run it. When I double click on the file it tries to open it up in dreamweaver but then asks for a serial number. I didn't even know I had dreamweaver on my mac and I don't have a serial number for it. Is there a way to have Komodo edit run the script? If not, what's the best way to execute it? Thanks in advance.
     
  2. samwich macrumors regular

    Joined:
    Aug 5, 2007
    #2
    PHP is a server side language, meaning "The code is interpreted by a Web server with a PHP processor module which generates the resulting Web page"

    So to test your PHP scripts, you have to run them on a webserver. I believe macs come setup to support PHP, but I have used MAMP (http://www.mamp.info/en/index.html) to set up a local webserver that I can test content on my personal computer.

    So you'd install MAMP, start the server, put your PHP script in a location in your htdocs directory and then visit the webpage by going to http://localhost/myscript.php in a web browser to view the results of your script.

    There may be other ways to accomplish this, but this is just one.
     
  3. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #3
    If it's just a "Hello World" file then you can open Terminal, cd to the directory containing your script and type:
    Code:
    php helloworld.php
    Since PHP is generally used for dynamic websites, though, what you probably want to do is run Apache server, with the PHP module enabled, on your machine and then access your app from a web browser.

    As another poster said, you can download 'MAMP' (Mac, Apache, MySQL and PHP) packages, but in fact Apache and PHP are already installed on OS X and can be enabled with a bit of terminal-fu: There are plenty of instructions for doing this on the web, e.g.

    http://lowendmac.com/crews/12lc/enable-php-mountain-lion.html

    If you want to use databases (common with PHP) then Lion and Mountain Lion already include the PostgreSQL database server (a quick Google should tell you how to get started) and older OS Xs include MySQL (which can easily be added to 10.7/10.8 if you prefer it). This is a nice, civilised thread so I won't invoke a flamewar by speculation on which is best...
     
  4. Ides thread starter macrumors member

    Joined:
    Mar 27, 2012
    #4
    @TheLuggage hey thanks that seems to work, sort of. Terminal lets me execute the script. Only when it runs it just prints out the source code. Am I missing something?
     
  5. theluggage macrumors 68030

    Joined:
    Jul 29, 2011
    #5
    You've probably forgotten the PHP tags around the code. Anything not surrounded by <?php ... ?> just gets printed out.
    Code:
    <?php
    echo("Hello World");
    ?>
    
    Or, you may have copied an example that just uses "<?" (without the "php") as the opening tag - you have to change a setting in the config file if you want to use "short tags".
     
  6. Ides thread starter macrumors member

    Joined:
    Mar 27, 2012
    #6
    You were exactly right, that was the problem. Many thanks!
     

Share This Page