Jan 19, 2021, 04:11 AM

News:

Choose a design and let our professionals help you build a successful website   - ITAcumens


Variables

Started by VelMurugan, May 20, 2009, 10:38 PM

previous topic - next topic
Go Down

VelMurugan

Variables

A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.

Another important thing in PHP is that all the statements must end with a semicolon ";". In PHP we needn't have to specify the variable type, as it takes the data type of the assigned value. The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement.

Here is some code creating and assigning values to a couple of variables:

Code: [Select]
<?php
//Commented lines starting with the double
//forward slash will be ignored by PHP

//First we will declare a few variables
//and assign values to them

$myText "Have a nice day!";
$myNum 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.
echo $myText;

//To concatenate strings in PHP, use the '.' (period) operator 
echo "My favourite number is "$myNum
?>


The output is: Have a nice day! My favourite number is 5.

VelMurugan

Case Sensitivity

One thing that causes many problems and take hours of finding mistakes is case sensitivity. PHP is case sensitive. Have a look at the following code:

Code: [Select]
<?php
$myVar 
"WebCheatSheet";
$myvar "PHP tutorial";

echo 
"$myVar - $myvar"//outputs "WebCheatSheet - PHP tutorial"
?>

VelMurugan

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.

One important thing to note if you are coming from another programming language there is no size limit for variables.

VelMurugan


Variable References


PHP also allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable. A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand. The following code snippet outputs 'Have a nice day!' twice:

Code: [Select]
<?php
$firstVar 
'nice day!';            //Assign the value 'nice day!' to $firstVar
$secondVar = &$firstVar;            // Reference $firstVar via $secondVar.
$secondVar "Have a  $secondVar";  // Alter $secondVar...
echo $firstVar;
echo 
$secondVar
?>

VelMurugan

Environment Variables

Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays.

Some of the environment variables include:

$_SERVER    Contains information about the server and the HTTP connection. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).

$_COOKIE    Contains any cookie data sent back to the server from the client. Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).

$_GET    Contains any information sent to the server as a search string as part of the URL. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

$_POST    Contains any information sent to the server as a POST style posting from a client form. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).

$_FILE    Contains information about any uploaded files. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated).

$_ENV
   Contains information about environmental variables on the server. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

The code to use the environment variables will be as follows:

Code: [Select]
<?php
// moderate shortcut
$newVar $_COOKIE["myFirstCookie"];

// full version
$newVar $HTTP_COOKIE_VARS["myFirstCookie"];
?>

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Kids Memories Writing :: Book Website @ 349 Rs monthly
Copyright 2005 - 2020 :: IT Acumens :: All Rights Reserved. :: Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Dedicated Server