Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cybix

macrumors 6502a
Original poster
Feb 10, 2006
993
1
Western Australia
Just wondering if anyone has some GOOD information on learning PHP? (for someone that doesnt know code)..

I did some simple programming in school, and can do some cut/paste type hacking, but have never really been a coder.

Perhaps some direction to a good source, a good book, a good site, tutorials, anything...

That'd be great!

James
 
There are a few websites plus O'Reilly has at least one excellent book on the subject. Google is your friend. :)
 
You're really in for a treat. PHP is simple but really satisfying to code, it's like putting a puzzle together and with each piece you get to fit you really start to understand the language more and more.

I'd just jump into it straight-up and maybe use w3schools.org as a source, they've got some good cheatsheets and guides.

I'm actually going to try to get some basic parts covered right in this post:

A span of PHP is really treated as a self-closing HTML tag: begins with a <?php and ends with a ?>, so a simple span of it might look like <?php print("Macs are awesome!"); ?>. The rest of a PHP document is treated like HTML; the <?php tells the server to start treating the code like PHP and ?> says "alright, treat the rest of this like unprocessed HTML."

Variables are preceded by a $ (dollar sign) and can't start with numbers. You don't need to explicitly initiate a variable; just the first time you use it, PHP knows it's a variable. To make a variable be something, use =, such as $mrmister="thebest". btw, text has to be in quotes to not be treated like a command or something else that might screw the PHP up. To test a variable without rewriting it, use == instead of a single =. Which brings me to if() statements:

An if statement is just a test of whether a condition is true. So if I've stated that $number=5 and then run the if statement

if($number==5)
print("five is an awesome number");

then "five is an awesome number" will be printed. If the statement had been if(number==4), then it wouldn't have worked. You can also include an else statement after it to tell it what to do if the condition isn't met:

$number=6

if($number==5)
print("five is an awesome number");
else
print("the number ain't five!");

In that case it'll print "the number ain't five!".

To print the current value of the variable, just say print($variable); if you want to print some text say print("text ololol blah blah");

I can't explain all of PHP in one post obviously but that'll get you started, also here are some things that'll stump you unless you know them because they're obscure but vital:

If you're printing something with quotation marks in it that you want to be visible, like "Denial ain't just a river in Egypt." - Mark Twain, then use ' instead of " to enclose it in print('text');.

If you want to include multiple actions as a result of an if statement, they have to be in brackets {}:

if($stuff="something")
{
print("woooo");
print("thing maybe");
$morestuff="leg";
};


You'll need to install a PHP processor (the part of Mac OS X's webserver that renders the code into HTML); Entropy.ch's PHP install is great. Just remember though if you're not accessing the files through a web browser from your Library/Webserver/Documents folder, they'll show as code and not the post-processed HTML.

I'd love to help you learn, ask all the questions you want and I'll try to answer them.
 
Mr. Mister, your detailed beginner's lesson reply to Cybix's question is an excellent example of how members here help each other.

I award you my "poster of the day" award (which I never remember to give out more than once a month). :)
 
Thanks. :) I might have made a few mistakes in there but as I said PHP is really intuitive and satisfying once you get into it.
 
Ya, Mr. Mister, awesome post. It has inspired me to use capitals in my post (just this once).

The official manual is here, but that may be a bit daunting :eek:.

So, I like this tutorial much better.

Note: The w3schools tutorial teaches echo instead of the print command that Mr. Miester showed you. They will both work, but have slightly different syntax.

I like to test my php applications locally before uploading them. This requires turning on your web server and installing php on your mac. First, go to system prefrences -> Sharing. Then turn on personal web sharing. Once it is started, see if it is working by going to "http://localhost/" in safari. If you see the Apache placeholder, it worked. Now install entropy's php module. Now you can place your php files in Macintosh HD-> Library-> WebServer -> Documents, and it will show up at "http://localhost/" with the php compiled.

I am glad I could help!

Edit: This was my 999th post by the way! I am catching up to mad jew!
 
Mr. Mister said:
If you want to include multiple actions as a result of an if statement, they have to be in brackets {}:

if($stuff="something")
{
print("woooo");
print("thing maybe");
$morestuff="leg";
};

:eek: :eek: :eek: :eek: bad code :eek: :eek: :eek: :eek: :p

Remember to always use double equals ( == ) for logical tests. The above is not incorrect, but behaves somewhat differently than you would expect. It basically tests the operation '$stuff="something"', ie if the parser is abble to assign the value "something" to the variable $stuff, then the logical test is true.

To complete this short post, one can also use triple equals ( === )! This is a step farther than ==, as it not only tests the value of the variable, but also its type:

$x = "6"; /* $x is a string */
$y = 6; /* $y is an integer */

if ($x == "6")
echo "true";
// this will print true

if ($y == "6")
echo "true"
// this should also print rue

if ($y === "6")
echo "true"
else
echo "false"
// this should print false as $y is the wrong variable type


Also, not need for semicolon after wavy brackets. ;)
 
Cybix said:
Just wondering if anyone has some GOOD information on learning PHP? (for someone that doesnt know code)..

I did some simple programming in school, and can do some cut/paste type hacking, but have never really been a coder.

Perhaps some direction to a good source, a good book, a good site, tutorials, anything...

That'd be great!

James

PHP is nice and easy, but be careful with it. It's a language that makes it real easy for somebody who doesn't really understand what they're doing to "just make it work"...the downside being that you do dangerous things.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.