The GTPlanet.net intro to PHP - Tutorials within!

  • Thread starter Thread starter Acid X
  • 7 comments
  • 788 views
Messages
6,668
United States
California
Messages
Axidwulv
The GTPlanet.net intro to PHP
Your instructor: Acid X (;))

Well, some people will have noticed if they have browsed this forum before, that I had some PHP tutorials up on an old website of mine. Well, that website is long gone, so I've decided to remake them here for all the people who are interested in learning some PHP. A sort of GTP Exclusive, per-say.

Anyway, what will be following will be a series of short but very easy tutorials (and very explanatory) for the novice PHP user, or for the person who just wants to learn the language. Eventually I may put up some more intermediate-level tutorials, but for now we will be sticking with the simple ones, just for the new people. ;)

Table of Contents: (Click the letter.)
A. Header, footer, variables, and the echo function
*Some additional statements from Giles Guthrie
B. If-Then-Else, and ElseIf
*More statements from Giles Guthrie

Notes: The table of contents will be updated whenever there is a new section added. If there is a section listed, and you do not see it below, it probably means i'm in the midst of typing it up.

Note 2: Feel free to leave comments and ask for help!
 
A. Header, footer, variables, and the echo function

A1. Header and footer
Alright, well, to begin a successfull script, one must always start with the correct header. For a server to recognize that a PHP file is actually a PHP file, it must be able to find the correct header to begin parsing (aka, reading and executing). The server must also know when to STOP parsing, which is why we have a footer as well! What's this correct header and footer, you ask? Well, that's what I'm here to tell you!

PHP:
<?php //Correct header
	//Script code...
	//And the footer below!
?>

Very simple, yes? You shouldn't have any problems with this part of a script (atleast, I'd hope not). Be sure to always include both of these, and keep the scripting between the header and footer tags. Anything outside of these tags will not be parsed by the server.. And will be visible to the user. Also, you can put PHP scripting anywhere in a file. You can have your basic HTML layout, and you can put some PHP within the file too. But if the filename of this file does not end with the right file extension (".php", ".php3", ".php4"), it won't get parsed, so be sure to always have the file extension correct if there is PHP in the page. Here's an example:

PHP:
<html>
<head>
<title>blah</title>
</head>
<body>
This is regular HTML stuff. <a href="blah.html">This is a regular HTML link, too!</a>

<?php //Notice this isnt in the beginning of the file? It doesn't need to be.
	//Script code..
?>

This is more normal HTML stuff. <a href="link.html">Linky, Linky!</a>.

<?php // Another header and footer? I smell more code!
	//Script code..
?>

</body>
</html>

Simple, yes? Also, in case you havent figured it out yet, the two forward slashes ("//") denote a comment. Anything after two of these slashes in a PHP script will not be parsed. It will only show up when viewing the code in an editor of some sort. Also, all PHP is parsed BEFORE the user even views the page. The server will parse the code, and then produce the result to the client when finished.

A common question asked by many people is whether or not the PHP code will show up in the users editor when viewing the source of a web page. The answer is NO. Your code is completely safe unless you give the file that you've created (physically via a file transfer or FTP transfer) to another person. I usually don't recommend doing this unless your giving the file to someone who is helping you. Not just someone who is claiming to help you, either.

A2. Variables
Alright, now here we are onto the variables section! Exciting, isnt it? Well, no, not really, but anyway, onto the point. The tutorial! Variables are basically the first block of PHP code. They're there to store the data. We wouldn't get very far without storing data in something, even if only temporary, right? Right. First of all, we'll start by teaching you how to define a variable.

PHP:
<?php //header!
	
	$variable1 = "string"; //Defining a variable (correctly)
	$variable2 = string; //Defining a variable (incorrect)
	$variable3 = 1920; //Defining a variable that is numerical (correct)
	$variable4 = "1920"; //Defining a variable that is numerical (ALSO correct!)
	$variable5 = $variable1; //[1] (later explanation)
	$variable6 = &$variable1; //[2] (later explanation)
	$variable7 = $variable3 + $variable4; //[3] (later explanation)
	$variable8 = function(); //[4] (later explanation)

?>

As you can see, a variable is defined by the dollar sign ("$") followed by the name of the variable. You can name a variable anything, but usually you should try to stay away from variable names that contain only numbers. A string (aka, any type of data other than numbers) should always be surrounded in quotes, which is why you see that I said (in the comment) that $variable2 is incorrect.

With $variable3, you see I am trying to define a variable with a numerical value. With numerical values, you do not need to surround it in quotes, even though you can if you wish (as you see in $variable4).

With $variable5, you can see instead of a numerical or a string value, I'm referring to $variable1 ([1]). When doing this, you are (basically) taking the information from the reference variable ($variable1) and copying it to the new variable. It's really very simple. Now, with $variable6, it's a bit different. As you can see, there is an ampersand before the reference variable this time [2]. The ampersand tells the script to actually referr to that variable, so instead of copying the data directly, the variable actually becomes more of an alias (aka, another name for the same thing) to the old one. Any changes done to $variable6 will also change the original, $variable1. Simple, eh?

Now, with $variable7 you can see im trying to solve a mathematical equation. With PHP, you can do mathematical equations and set the resutl to a variable. With $variable7, I am adding $variable3 and $variable4, which are both basically the same number, and $variable7 would end up being set to 3840, which is the result of the added equation. A variable can also be set to the result or return value of a function, as seen in $variable8 [4]. Functions that have a return value will set the variable to that value. Simple.

Note: Always remember to end ANY line with a semi-colon (";"). The only exception to this is if the line ends in a bracket ("{" or "}").

A3. The echo function
This is a really simple one, so I'll keep it short. The echo function is what prints something out for a user to see. Here is an example.

PHP:
<?php //header :)
	
	echo "This is what the echo function does.";

?>

This would make a user see "This is what the echo function does," whenever they visit that PHP file. The function is followed by either a string (in quotes!), an integer (numerical value, quotes or not), a mathematical equation (result), or the result of a function (that gives a return value). Also, you can have it echo out the value of a variable like so:

PHP:
<?php //header

	$variable = "This is the contents."
	echo $variable;

?>

Very simple. If you need more of an explanation, send me a pm!

One last note!
For strings, you may surround them in either these: ', or these: ". If you plan on using the same type of quote within the string, you need to cancel it out. Like this:

PHP:
<?php

	echo "This is a \"sentence that contains quotes.\" Beware.";
	echo 'This is a \'sentence that contains quotes.\' Beware.";
	// No need for cancellation if the quotes are different. Like so:
	echo "This is a 'sentence that contains quotes.' Beware.";

?>

This is called "Escaping," and is rather simple. Simply put a single backslash ("\") in front of the character that needs cancellation.

Expect my next tutorial very soon! Comments are welcomed!
 
Nice start Acid X!

Couple of notes:

When you refer to the <?php ?> tags, you seem to get in a bit of a flap trying to explain the difference between what's in the tags, and what's not. You may want to tighten the wording up a little.

Also, it may help if you discuss the timing - i.e. that all within <?php ?> is read and interpreted by the server before it goes to the browser. You hint at this in the source code question, but I think it may help people's understanding if you explain this. Also helps people to understand how PHP makes pages dynamic, and how it is different from client-side languages like Javascript.

You have a typo. The ";" character is called a semi-colon, not a colon. The colon is rarely used in PHP. In fact, I can only thing of one instance, in a switch() case: statement.

In discussing use of the "echo" command, you may feel it useful to state that you can "drop out" of PHP at any time. This is more efficient than echoing a large code block to the browser.

If you're not sure of this, it works thus:

PHP:
<?php
if($somevariable == "some value")
{
?> Lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots of HTML<?php
}
?>

It also makes the code easier to edit, as you can see the bits of plain HTML in your editor. I tend only to use echo when I'm sending variables to the screen.

Finally, the word you're looking for is "Escape", not "Cancellation".
 
Mr. Perfect over here.. Hehe.

Yes, "Escape", not "Cancellation".. As soon as i get back into the loop, i'll go back and fix the little errors you've pointed out. I think i was tired when writing that, which is why i said colon instead of semi-colon.

I'll also elaborate on the "drop out" thing. I knew about this, but was hesitant to talk about it as i thought it might confuse people..

Anyway, i'll fix all that (and a bag o' chips) later.
 
B. If-Then-Else, and ElseIf

B1. If-Then-Else
Okay, so, moving on to the next tutorial, we are now going to be dealing with If-Then-Else statements. Fun, huh? I think so.. Heheh. Anyway, If-Then-Else statements are very easy to comprehend. Here is how an if-then-else statement should look. Can you figure out how it works?

PHP:
<?php
	if (A == B) {
		C
	} else {
		D
	}
?>

Figured it out yet? No? Well, either way, this statement is basically saying, "If A is equal to B, then do C. If not, then do D instead." Simple, no? Yes! Here's another basic example:

PHP:
<?php
	$smuckers = TRUE;
	if ($smuckers == TRUE) {
		echo "Smuckers is true!";
	} else {
		echo "Smuckers is not true!";
	}
?>

This is saying, "If variable $smuckers is true, then say 'Smuckers is true!' But if smuckers is not true, as in, it's false, or some other odd value, say 'Smuckers is not true!'" It's relatively simple.. Though, in this case, there would be really be no need for the if statement, because $smuckers will always be TRUE, since that's how the variable is defined.

See now? If statements work like this; If the statement is TRUE, continue to the THEN, if not, then go to the ELSE. Here's a full breakdown:

PHP:
<?php
	if (statement) {
		//THEN DO THIS
	} else {
		//DO THIS
	}
?>

Get it? Alright. Now i'll show you how you'll need an if statement for that value $smuckers.

PHP:
<?php
	//Checking another variable
	if ($variable > 24) {
		$smuckers = TRUE;
	} else {
		$smuckers = FALSE;
	}

	//Now checking $smuckers
	if ($smuckers == TRUE) {
		echo "Smuckers is true!";
	} else {
		echo "Smuckers is not true!";
	}
?>

Now $smuckers value depends on the value of another variable, that is defined somewhere else. Possibly from a form or something, but this is an example, so we'll just imagine where it comes from. The beginning if statement checks to see if $variable is a number greater than 24. If the number is less than, equal to the number 24, or is not even a number what-so-ever, then it will continue to the else, and set $smuckers to false. But if $variable is indeed higher than the number 24, it will be set to true. Simple, eh?

And that's it to an if-then-else statement.

On a quicky note, "TRUE" and "FALSE" are actually usable values. They do not have to be surrounded in quotes, because PHP recognizes them as what they are. True and false.

ElseIf
ElseIf is simply an extended if statement. It is coupled with a normal if statement. The ELSEIF comes before the ELSE, but after the beginning IF. Like so:

PHP:
<?php
	if (statement) {
		//THEN
	} elseif (statement) {
		//THEN
	} else {
		//ELSE
	}
?>

The elseif if ONLY called on if the original IF is false. PHP will move from the IF to the ELSEIF, and then continue to the ELSE if the ELSEIF is also false. Here's an example:

PHP:
<?php
	if (1 > 2) {
		echo "One is greater than two!";
	} elseif (1 == 2) {
		echo "One is equal to two!";
	} else {
		echo "One is not greater than two, nor equal to two!";
	}
?>

The above would result in "One is not greater than two, nor equal to two!" Because, as we all know, 1 is NOT greater than two, nor is it equal.

PHP:
<?php
	if (1 > 2) {
		echo "One is greater than two!";
	} elseif (1 < 2) {
		echo "One is less than two!";
	} else {
		echo "One is not greater than two, nor less than two.";
	}
?>

Now, the above code would result in "One is less than two!" Since we all know that one is obviously less than two. The ELSE would not be called, because one of the two statements were true. If the first statement ("1 > 2") was true, then the ELSEIF would be ignored, as a true statement was found already.

It's rather simple, and you can have as many ELSEIF's as you like in a script. PHP will check each one before continuing to the next. If one ELSEIF is true, the rest will not be called. Simple aye?

Also, as reference, here is an article on the official PHP web-page (www.PHP.net) about "Control Structures." You'll find all information that you need about operators for IF statements within.

Click to read

Remember, any questions, go ahead and ask! :)
 
Yep. Another good one. :)

The "double equals" sign is worth explaining. When testing for equivalency ("does 'black' = 'black'?") you have to use two equals signs together. This is because a single equals sign is interpreted by PHP as an instruction.

PHP:
<?php
// Set up the variables. A should be 5, B should be 8
$a = 5;
$b = 8;

if($a == $b)
{
  echo "A equals B<br>";
}
else
{
  echo "A does not equal B<br>";
}

if($a = $b)
{
  echo "A equals B");
}
?>

Will output:

A does not equal B
A equals B

Because in the second if() statement, the operator is actually setting $a equal to $b, before the if() operator. Therefore a single equals sign in an if() statement will always evaluate to TRUE.

In your previous tutorial, the difference between the .php, .php3 and .php4 extensions is thus:

".php" will be executed using whatever version of PHP the server is running.
".php3" will be executed as PHP v3 by a PHP v3 server and a PHP v4 server.
".php4" will be executed as PHP v4 bya PHP v4 server, but will be ignored by a PHP v3 server.

Largely speaking, the number is unrequired.
 
Ah-ha! You have a problem there Giles. Re-read your first if-statement. You're checking to see if a is equal to b, but in fact, its not, so "A is not equal to B" would NOT be printed out.

Might want to fix that. ;)
 
Acid X
Ah-ha! You have a problem there Giles. Re-read your first if-statement. You're checking to see if a is equal to b, but in fact, its not, so "A is not equal to B" would NOT be printed out.

Might want to fix that. ;)

Good spot! Cheers!
 
Back