Exforsys.com
 
Home Tutorials PHP
 

PHP Tutorials - Variables

 
Category: PHP
Comments (1)

Variables

In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.


Variables

In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.



Identifiers: 

Identifier is the name of the data, identifiers also can be used as a label for a set of commands.


There are rules for identifiers naming, rules are:


• The first character has to be a letter or an underscore (_);
• The following characters can be any combination of, letters, digits, and undersore.


Example:


The following are valid identifiers:


identifier
_identifier
__identifier
_another_identifier
another_identifier


Variables:

A variable is a data container, it can contain data of any of the types stated before.
A variable can contain only one value at a time.
A variable is an identifier preceded by a dollar sign ($).
Variable names are case sensitive, for example, $var is different from $Var.
Variables can be embedded directly in strings, for example, “Value \$var is embedded here”


If there is no space between the variable name and the rest of the string, variable can be embedded like in the following example:


<?php
$var = 100;
echo “There are {$var}000 books in the college library”;
?>


Statements:

A statement is a command that is executed, for example the echo function causes an expression to be sent to the standard output.


Statements has to be ended with a semicolon.


Constants:

A constant is a data container like variable, but the data value of the constant doesn’t change.
To create a constant use the construct define().


Example:


<?php
define(“MY_CONSTANT”, 100);
echo MY_CONSTANT;
?>



define() takes two arguments, the first is a string, which is the constant name, and the another is the constant value.


There is a little trick about constants, they can be defined with any name, but they can’t be used unless they follow the identifier naming rules.



Read Next: PHP Tutorials - Operators (Part I)



 

 

Comments


jobo said:

  If you try to cut and paste the sample codes on variables and constants you will recieve error on your screen. The reason is unclear but just the same, to solve the problem, key-in manually the sample codes and it will run. *It seems the double quotes are the problem..
February 19, 2008, 12:26 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape