Your full service technology partner! 
-Collapse +Expand
Search PHP Group:

-Collapse +Expand PHP Store

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PHPBeginners Co...   Print This     
  From the March 2016 Issue of Prestwood eMag
PHP Beginners Corner:
PHP Variables ($x = 0;)
Posted 14 years ago on 11/13/2008 and updated 1/28/2009
PHP Code Snippet:
 A flashcard from our PHP Flashcards Library
 A code snippet from our PHP Code Snippets Page
 Tags: PHP , Variables


Languages Focus: Variables

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

PHP Variables

PHP is a loosely typed language. No variable types in PHP. Declaring and using variables are a bit different than in other languages. In PHP, you identify and use a variable with a $ even within strings!

You assign by reference with & as in &$MyVar.

Syntax Example:
$fullname = 'Mike Prestwood';
$FullName = 'Wes Peterson'; //This is a different variable!
$Age = 38;
$Weight = 162.4;

echo "Your name is $fullname.
echo "You are $Age and weigh $Weight.

Some PHP Examples

Just like HTML, quotes are single or double:

echo "Mike's drums are over there.<br>";
echo 'Mike said, "hi!"<br>';

You don't specify the variable type, the interpreter will automatically use a variant-type variable. To Declare, just assign:

$fullname = 'Mike Prestwood';
$FullName = 'Wes Peterson';
$Age = 38;
$Weight = 162.4;

//Variable within a literal.
echo "Your name is $fullname.<br>";
echo "You are $Age and weigh $Weight.<br>";

//$ within a literal ok too.
echo "That will be $1.52.<br>";


PHP is case sensitive with variables too:

$fullname = 'Mike Prestwood'; //This is different...
$FullName = 'Wes Peterson';   //than this.

echo $fullname;
echo $FullName;

By Reference uses "&" as in:

$MyOriginalVar = "Mike";
$MyNewVar = &$fullname;

echo $MyOriginalVar; //Mike
echo $MyNewVar; //Mike

$MyNewVar = "Lisa";
echo $MyOriginalVar; //Lisa (original changed!)
echo $MyNewVar; //Lisa

More Info


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P163A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101615 Counter
Since 11/13/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2022 Prestwood IT Solutions.   [Security & Privacy]