So you want to start programming in PHP, here's a couple of starters and links that can help you on your way...
As with every programming language, the "hello world" example is used as the first script. This tutorial will be no different ":D
To display items, we will use the "echo" function, there is also the "print" function, but i prefer echo ":D
so...
Hello WorldCODE
<?php
echo("Hello World\n");
?>
Your first php page is now complete! You can do that in notepad if you want, as long as you have the php backend installed, it will be processed and displayed. Just remember to save it with the .php extension.
You might be wondering what the \n does - it's a break, which says that a new line must be used. Also, the ; is placed at the end of each line, otherwise you will get errors along the lines of...
QUOTE("Error")
Parse error: parse error, unexpected T_VARIABLE in f:\work\webpages\script.php on line 15
Now to get a bit advanced...
We going to start using variables...
The VariableCODE
<?php
$var = "Hello";
$var2 = "World";
echo("$var $var2 \n");
?>
Now you see a period (.) - A . in php joins things. So if i was to say -
$var = "Hello";
$var .= " World";
when echo'ing $var, you'd get the entire "Hello World" effect.
Also, you may see that everything being echo'ed is encased in "'s, this is so that the value of the variable is given and not just the word $var. For example...
CODE
<?php
$var = "ROAR";
echo("I like to $var<br>\n");
echo('I like to $var');
?>
This will output the following...
QUOTE
I like to roar
I like to $var
You see that i have used an html tag in there, with php you can use all the standard html tags, seeing as the page is displayed in html format (just has a .php extension). <br><b><u><font> etc. all those can be used.
When using a URL in a string, one must be careful seeing as it is an html tag that uses "'s to get the value itself. This problem is solved in three ways...
The SolutionDouble quotes within single quotes
CODE
<?php
$url = '<a href="www.yoursite.com" target="_blank"></a>';
echo("$url\n");
Variables using single quotes
CODE
<?php
$var = 'MOO!';
$link = '<a href="www.somesite.com">' . $var . '</a>';
echo("$link\n");
?>
Using escape characters
CODE
<?php
echo("<a href=\"www.yoursite.com\" target=\"_blank\"></a>\n");
?>
So now you have some basic knowledge on setting variables, and displaying them, as well as getting around some obstacles.
The only thing with using the single quote, some of the escape characters such as \n will not be executed, but rather displayed, in this case you would have to use the <br> alone
How's about using conditions to determine what will happen?
The IfIf you have any other programming experience, you know what the if is about. It basically checks to see if what you are saying suits the condition. The condition can also be manipulated into a false (If not etc.). There is a right, and a wrong way to format the if statement, not only does it make it easier to see what you have done, it also makes the code better looking. The code below is formatted in the generally accepted manner.
CODE
<?php
$var = 1;
if($var == 1){
echo("There is a match, the statement matches the condition");
}
else{
echo('There is no match, the statement does not match the condition of $var<br>');
};
Following the if is, the compound if statement, also known as the case statement.
The Case StatementWhat this does is save you from having to right 39 if statements using the same variable in the condition.
Case vs. If ExampleCODE
<?php
// Seed random number generator
srand((double)microtime() * 1000000);
// Pick a random lowercase letter and announce it
$letter = chr(rand(97, 122));
echo("The letter is '" . $letter . "'<br>");
echo("Single if: ");
if($letter == "a" || $letter == "e" || $letter == "i" ||
$letter == "o" || $letter == "u"){
echo("The letter is a vowel!<br>");
}
else {
echo("The letter is NOT a vowel!<br>");
}
echo("If-elseif-else: ");
if($letter == "a") {
echo("The letter is a vowel!<br>");
}
else if($letter == "e") {
echo("The letter is a vowel!<br>");
}
else if($letter == "i") {
echo("The letter is a vowel!<br>");
}
else if($letter == "o") {
echo("The letter is a vowel!<br>");
}
else if($letter == "u") {
echo("The letter is a vowel!<br>");
}
else {
echo("The letter is NOT a vowel!<br>");
}
echo("Switch: ");
switch($letter)
{
case "a":
case "e":
case "i":
case "o":
case "u":
// Notice if $letter is not 'u', it 'falls' through to here
echo("The letter is a vowel!<br>");
break;
default:
echo("The letter is NOT a vowel!<br>");
break;
}
?>
Until now, we have been using native functions, but sometimes there is not a funciton designed for what you need. So you need to code your own.
The FunctionAs with everything, there is a proper way to call a function.
The right way --> $var = calcadd($n1, $n2);
The wrong way --> $var = calcadd($n1,$n2);
now to code the function...
CODE
<?php
$n1 = 4;
$n2 = 7;
function calcadd($n1, $n2){
$n1 = $n1 + $n2;
return $n1;
};
$call = calcadd($n1,$n2);
echo("$call");
?>
This is a very basic tutorial that should allow you to start somewhere and gain experience.
Here are some helpful links...
123I borrowed the format of this tutorial from link 3, but i changed the content incase you noticed a similarity
This post has been edited by ::DocterMoo::: Jun 18 2004, 04:37 AM