PHP: A Beginners Guide - Part 1
Tuesday, August 26th, 2008 | PHP
I would like to start by stating there are many websites on the net which have very similar tutorials on PHP, and pretty much everything you can think of. People, however, also learn in many different ways. I am putting this guide together in a fashion which would have best helped me learn when I was starting out. That said, let’s begin.
PHP has both an opening and closing tag. These tags usually begin at the first line of code, and are placed in a file with an extension of .php:
<?php
?>
The “echo” command is used to display output in PHP:
<?php
echo(“Hello World!”);
?>
Output:
Hello World!
Note how the echo command ends in a semicolon (;). All lines in PHP should end with a semi-colon except for opening and closing PHP tags and those which begin and end with brackets ( {} ). (ex: loops, if/else statements, functions, classes)
Variables in PHP are defined by the $ symbol. All variables are loose variables, and do not need a special typecasting, though available and is generally a good practice (details later). PHP also does not require variables to be declared before being initialized. For example:
<?php
$myString = “This is a string”;
$myInteger = 0;
$myDouble = 1.2345;
?>
You can echo your value using your variable name. Values are appended using a period (.). For example:
<?php
$myString = “This is a string”;
$myInteger = 0;
$myDouble = 1.2345;echo “Your string is: ” . $myString;
echo “<br />“;
echo “Your integer is: ” . $myInteger;
echo “<br />“;
echo “Your double is: ” . $myDouble;
?>
Output:
Your string is: This is a string
Your integer is: 0
Your double is: 1.2345
That’s all for today. Tomorrow I will go over how to use PHP within XHTML. If there is any confusion, feel free to respond.
1 Comment to PHP: A Beginners Guide - Part 1
Excellent tutorial. Thanks for taking the time to do this. I know many people will discover this and find it helpful. Can’t wait for more! Going to go practice what I just learned.
August 26, 2008