How to declare a variable in PHP

Thursday, July 28, 2011 · Posted in

It is one of the most common error of a programmer in PHP which is on how to declare a variable. Every programming language recquires varaiable declaration because variables are being use to store your temporary data. Sometimes programmers for got and sometimes failed to declare a variable in which it causes an error to their programs. The dollar sign symbol ($) uses to declare a variable in PHP. In other web programming language such as Coldfusion it uses double number sign (##). For PHP case $ is the only one that being use to declare a variable and no other than. Below is the sample of variable declaration and together with its output.


PHP Code:

<?php
$firstname = "John Larry Limbo";
$middlename = "Abellanosa";
$lastname = "Limbo";

echo "My firstname is: ".$firstname."<br/>";
echo "My middlename is: ".$middlename."<br/>";
echo "My lastname is: ".$lastname;
?>

Dont for get to put the php opening ang closing tags before you run the code cause it might cause an error on your program. As you notice I use dot(.) on my code. The function of this dot is to concatenate the string “ My firstname is: ” with the value of the variable $firstname and same with the other. Observe carefully the ouput below or you may try it with your own. Just send me a comment if the code does not run or it returns an error.


Sample ouput:


If there are some comments or questions about this article just CLICK THIS LINK to be redirected to the forum page.



» More Articles can be found in this site. Click Here

Powered by Blogger.