PHP - Your First PHP Script
From Global Programming Syntax
PHP is a relatively simple language to learn. It is based on pre defined functions which can lead to great results. But first is first. Just like every other language, the "hello world" script comes first.
The "hello world" script
First lets get familiar with the echo function. It basically sends text to the browser. However when writing any php script, you always need the php opening tag and at the end of your script you need the php closing tag. Below is an example:
<?php
echo '<h1>Hello world!</h1>';
?>
Next lets make hello world repeat all the way down the side of the screen. To do this, you will need a for loop. First lets observe the required code:
<?php
for ($id=0;$id<30;$id++) {
echo "Hello world.<br>\n";
}
?>
Now at first the above code will look a bit scary but when explained properly, it will make a lot of sense. First on line 1, as you can see we have the standard php opening tag. This tells the computer lets stop doing html and start doing php.
Then on the next line is whats called the condition of the for loop. The condition is separated into 3 parts each separated with the ; character. The first par $id=0 tells the computer what to do before the very first loop. So the code in the first part only ever happens once. Then the middle section $id<30 is the statement saying how long the loop loops for. So basically the second section is like an if statement and if the second part is true or passes the statement then another loop will occur. Then in the last section is $id++. This happens each time the loop goes through the cycle. The $id++ tells the computer the add the number one to the variable $id. So to explain the entire concept as a whole, just before the first loop begins, $id is set to zero. Then every time the loop goes through a cycle, $id adds the number one to itself. Then it keeps on appending/adding hello world to the document until $id equals 30. And in the end, the result will be Hello world being display 29 times on 29 lines.
Making Functions
Lets make an display text function that will display raw html code on the page. First the following code will need to be used:
function html($text) {
echo htmlentities($text);
return true;
}
html('<b>Some text <i>and</i> code.</b>');
The above function acts like an echo function except no html will run. Instead the html will display as typed. So instead of that example displaying the text in bold, it will display the actual tags on the screen. You may wonder, how was that html function made? Well as you can see on the first line, there is the word 'function' then the function is defined. You can name this function whatever you want as long as it does not already exist. For example the htmlentities function had already existed so that name could not be used. Then between the brackets are optional variables that you can specify. There variables are the input elements which are each separated by a coma. So below is an example of another function
function test($text,$number=1) {
$returntext='';
for ($id=0;$id<$number;$id++) {
$returntext.=$text."\n";
}
return $returntext;
}
echo test('testing');
echo test('another',3);
Above outputs: testing another another another
With the above function, you will see that the first field of the test function must be specified, however, the second parameter is optional. That is because of how the first line was defined. If you compare how each of those parameters were defined, you will see that with the $number parameter, it was set so $number=0 by default. This can be useful if the defaults are commonly used. And you will notice that with every function there is a return line. The data which is returned is what can be retrieved by doing something like $variable=test('asdf');
Newbies Quick Video Guide
[video src=php_newbies_guide size=600x474]
