TextWrangler and PHP Problems

Discussion in 'Mac Programming' started by bLaZ3n, Mar 5, 2014.

  1. bLaZ3n macrumors regular

    Joined:
    Jul 9, 2010
    #1
    First things first, I know it isn't Java, C or Xcode.. but I'm new to programing on a MAC OS. I'm used to a windows environment. But, how do I run a php/html based program in Text Wrangler?

    I get his error:
    PHP:
    This file doesn’t appear to contain a valid ‘shebang’ line (application error code13304)
     
  2. D.T., Mar 5, 2014
    Last edited: Mar 5, 2014

    D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #2
    I guess I’m not totally clear on what you’re doing. If you’re trying to run PHP on your Mac, you’ll need a web server that will handle PHP.

    If you’re just starting out, I’d recommend MAMP, which is a Mac AMP stack (Apache [Web], MySQL [DB], PHP [extension for Apache to handle PHP]).

    Then with TextWrangler (great text editor BTW), you’ll edit your code, then browse it with Safari/Chrome/etc., on the MAMP configured IP/port (example: http://127.0.0.1:8000)

    If you’re just trying to use the RUN command from the TW menu, that’s just trying to invoke a shell script (hence the “shebang" error, i.e., the #! expected when invoking a shell). TextWrangler is _just_ a text editor (not a full IDE like Visual Studio), it doesn’t have a compiler, debugger, web services or anything like that.

    Conceptually, it’s not that much different if you’re familiar with Windows based web platforms like .NET - you still need a local web server with .NET extensions. It just happens to be bundled into VS, so the RUN command in VS, creates an instance of a web server, with your application as the root, then points your default browser to it, so it’s very automagic (though when I’m working in .NET, I have an IIS instance running non-stop, and generally just swap out to my browser vs. letting VS fire up an instance each time).

    AMP stack download for OSX:

    http://www.mamp.info/en/
     
  3. bLaZ3n thread starter macrumors regular

    Joined:
    Jul 9, 2010
    #3
    Okay I'll try.. I slightly understand and I already have MAMP installed and running

    ----------

    Okay, where do I put my .php files so I can browse through chrome? I looked through the MAMP folder and I don't see a root or www directory
     
  4. D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #4
    When you run MAMP there’s a small UI that fires up (in addition to the AMP admin page you’re directed to). In that UI, go to Prefs, Apache, and that will define the document root:
     

    Attached Files:

  5. D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #5
    ... and that same UI/config will show you the port the web server is running on, so in this example, it’s http://localhost:8888
     

    Attached Files:

  6. bLaZ3n thread starter macrumors regular

    Joined:
    Jul 9, 2010
    #6
    I have it set to default.. but for some reason I can't browse to the file through chrome.. can you help me construct the URL?
     

    Attached Files:

  7. D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #7
    OK, so you’ve got your webroot, easy enough, and your port should be on that PORT tab in the screenie you posted. Since it’s local, if you haven’t done any custom hosts or anything, you can use localhost:<port>, or 127.0.0.1:<port> (where port = the value on your port tab, that I believe is 8888 as a default).

    If you point a browser to that URL what do you get? Some browser message like a nonexistent URL, or a PHP error? If directly browsing is on, you should see the root of your web server and the files, or if you’ve got a default document (index.htm, index.php), you should see that file.

    If you’re getting _some_ kind of response, then maybe try a static HTML file at the root and go directly to it. If you’re getting a PHP related error, you can always toss in an test page like:

    PHP:

    <?php

    phpinfo
    ();

    ?>

    Which will dump your PHP environment when you browse that page.


    Slightly off topic: PHP is _evil_ :D
     
  8. bLaZ3n thread starter macrumors regular

    Joined:
    Jul 9, 2010
    #8
    I got it.. silly me.. I was using the wrong ports. I did "localhost:<Apache port>" .

    Thanks for your help
     
  9. D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #9
    Awesome. Yeah, sometimes it's something painfully obvious.

    BTW, if you're just getting into PHP you might want to consider a good MVC framework - it'll reinforce some good dev behaviors and ultimately speed up development. CakePHP, Yii, there's several great options.
     
  10. bLaZ3n thread starter macrumors regular

    Joined:
    Jul 9, 2010
    #10
    Just wondering are those programs you recommended applications to build php programs? Sort of like Eclipse for Java?
     
  11. D.T. macrumors 604

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #11
    Those products I recommended are actually frameworks (not as IDE like Eclipse, Visual Studio, etc.) A framework sits “on top” of a language and extends the functionality - some frameworks extend languages to make them very web friendly (Ruby > Rails, Python > Django), but some just provide a whole slew of helper functions, wrappers, etc., to simplify things things which is the case here.

    PHP is easy to write sloppy, to not use an MVC pattern, so there’s data access in controllers, and code strewn all through markup (views), etc. Plus, there’s a ton of repetitive chores in dealing with things like databases, lists/tables of data and whatnot. These PHP frameworks try to provide most of this.

    BTW, I just realized this section says “not for web programming” since I guess that’s not necessarily mac related.
     
  12. murrayE macrumors member

    Joined:
    May 4, 2011
    #12
    Did you look up "shebang"? In the case of PHP, it's the character sequence
    "<?" that should begin in the file; the last line of the file should be the corresponding "?>".

    You don't need any web page or web server to run a PHP program, just the PHP interpreter, which should ordinarily be on your path. Write and save your php file from any text editor, including Text Wrangler. Then run it from the command line. For example, look at the following file prog.php:
    Code:
    <?
    echo "Hello, World!\n";
    echo "1+1 equals ",1+1,"\n";
    ?>
    
    Then at the Terminal command line:
    Code:
    [~/mydir]$ php prog.php
    Hello, World!
    1+1 equals 2
    
    It's standard to replace the opening shebang sequence with "<?php".

    Of course, the most common use of PHP is to execute scripts within a web page. but that's not what you actually asked.
     

Share This Page