Ok so well i've known PHP for a while, i'ld say i was quite good at it but still have things to learn, but anyway here's a noob's introductory to PHP which will cover all teh basics and introduce all the good coding practices. By the end of this tutorial it will mainly be a matter of learning the different functions and their uses.
II. PHP Introduction
Right PHP is basically a server side programming language which can be used to build dynamic sites. Because it's server side no person can steal your code without access to your server. In comparison to markup languages like HTML where you can just view the source.
III. Syntax
PHP always needs to have a .php extension (although it can be .php5 for example depending on certain server requirements). Whether you simply include PHP within a HTML file it still needs the .php extension.
Now PHP requires opening and closing tags, mainly so that thigns can be differentiated.
?>
are the opening () and closing (?>) tags needed. At the end of every statement in PHP there must be a semi-colon ( wink . PHP doesn't pay attention to new lines or empty lines, so the semi-colon is used to differentiate statements.
wahmbulance Good Practice:
Now for good practice you should make your coding clear and easy to read as well as understand. teh opening and closing php tags should be on their own lines. You should put every new statement on its own line, seperating them with an empty line break where they do not tie in together.
IV. Echo Data
This is the most basic function available in PHP (IMO). It follows the following syntax:
echo 'text';
notice a few things in there. the keyword is echo, then its the text and it's finished with a semi-colon (indicating the end of the statement). also notice the single quotes rather then double.
the above would simply output the following:
text
V. Variables
Variables are one of the most important things in PHP. Variables (in case you dont know) hold values which can make things dynamic.
All variables in PHP have a preceeded $ sign.
example of variable names are:
Quote:
$hello
$test
$i
$j
$i1
$i10
all of those are valid variable names. a variable may never begin with a number, so $1i is invalid.
Now variables can be passed into functions, let's use our familliar echo:
$temp_var = "hello";
$temp_var_2 = "hello 2";
echo $temp_var;
echo $temp_var_2;
?>
teh above would output:
Quote:
hellohello2.
Now we could echo them both out at the same time, if we replace the two echos with:
echo $temp_var . $temp_var_2;
teh same thing would be echoed out it just saves us a line of coding. Note the dot (.) is used to seperate teh variabels from each other. We can futher extent this by changing the echo now with:
echo $temp_var . '
' . $temp_var_2;
the
(line-break) in the middle is treated the same as the variables and needs the dot(.), the above would output:
Quote:
hello
hello2
hello2
as the br puts the second variable onto the next line.
This can be extended even more. surprised blaugh
if we replace the last echo with
echo "$temp_var
$temp_var_2";
now the double quotes do a bit mroe processing hten the single quotes, (which is why single quotes are faster when not using variables). The variables will be replaced with their values and just put straight into the string, therefore removing all those dots which will get confusing over time.
VI. Comments
wahmbulance Good Practice:
Comments are all about good practice. It means you can make notes on what your doing for other devs or just for yourself, they are not parsed by teh PHP engine but they can take up memory.
Comments can be declared in two ways:
//
for a single line
/* */
for multiple lines
i suggest using option 2 as it's generally better cool
You use it as follows:
/* This does this */
very simple and very effective smile
VII. Conclusion
Now there is much more to learn about PHP, hell this is the very start, but once you learn good practice and the very basics PHP just gets easier xd , i may add more later but i think this can get anybody started. Once you've read and played about with all the thigns i've said move to WCSchools or Tizag are both great IMO.
best of luck
mrgreen