Wednesday 11 July 2012

PHP - Variables with Example tutorial


PHP - Variables
If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you. A detailed explanation of variables is beyond the scope of this tutorial, but we've included a refresher crash course to guide you.
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
• $variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!
A Quick Variable Example
Say that we wanted to store the values that we talked about in the above paragraph. How would we go about doing this? We would first want to make a variable name and then set that equal to the value we want. See our example below for the correct way to do this.


PHP Code:

$hello = "Hello World!";
$a_number = 4;
$anotherNumber = 8;
?>
Note for programmers: PHP does not require variables to be declared before being initialized.
PHP Variable Naming Conventions
There are a few rules that you need to follow when choosing a name for your PHP variables.
• PHP variables must start with a letter or underscore "_".
• PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
• Variables with more than one word should be separated with underscores. $my_variable
• Variables with more than one word can also be distinguished with capitalization. $myVariable


PHP - Echo
As you saw in the previous lesson, the PHP function echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo function more than any other. So let's give it a solid perusal!
Outputting a String
To output a string, like we have done in previous lessons, use the PHP echo function. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.


PHP Code:

$myString = "Hello!";
echo $myString;
echo "
I love using PHP!
";
?>
Display:
Hello!
I love using PHP!
In the above example we output "Hello!" without a hitch. The text we are outputting is being sent to the user in the form of a web page, so it is important that we use proper HTML syntax!
In our second echo statement we use echo to write a valid Header 5 HTML statement. To do this we simply put the
at the beginning of the string and closed it at the end of the string. Just because you're using PHP to make web pages does not mean you can forget about HTML syntax!
Careful When Echoing Quotes!
It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! The echo function uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:
• Don't use quotes inside your string
• Escape your quotes that are within the string with a slash. To escape a quote just place a slash directly before the quotation mark, i.e. \"
• Use single quotes (apostrophes) for quotes inside your string.
See our example below for the right and wrong use of the echo function:


PHP Code:

// This won't work because of the quotes around specialH5!
echo "
I love using PHP!
";
// OK because we escaped the quotes!
echo "
I love using PHP!
";
// OK because we used an apostrophe '
echo "
I love using PHP!
";
?>
If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the quotations by placing a slash in front of it ( \" ). The slash will tell PHP that you want the quotation to be used within the string and NOT to be used to end echo's string.
Echoing Variables
Echoing variables is very easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.


PHP Code:

$my_string = "Hello Bob. My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>
Display:
Hello Bob. My name is: 4a
Echoing Variables and Text Strings
You can also combine text strings and variables. By doing such a conjunction you save yourself from having to do a large number of echo statements. Variables and text strings are joined together with a period( . ). The example below shows how to do such a combination.


PHP Code:

$my_string = "Hello Bob. My name is: ";
$newline = "
";
echo $my_string."Bobettta".$newline;
echo "Hi, I'm Bob. Who are you? ".$my_string.$newline;
echo "Hi, I'm Bob. Who are you? ".$my_string."Bobetta";
?>
Display:
Hello Bob. My name is: Bobetta Hi, I'm Bob. Who are you? Hello Bob. My name is: Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta
This combination can be done multiple times, as the example shows. This method of joining two or more strings together is called concatenation and we will talk more about this and other forms of string manipulation in our string lesson.

No comments:

Post a Comment