Welcome to Egypt Forums Mark forums read | Egypt Main Page
Egypt Forums
Arabic Movies



Classes & Source Code Thread, Variables in PHP in PHP; Variables in PHP Variables are probably one of the most commonly used code constructs you’ll come across while programming. If ...

Short Link: http://forum.egypt.com/enforum/showthread.php?t=6989


Reply
 
 
The God Father
Developer's Avatar

Reply With Quote
 
Join Date: Jul 2008
Location: NDC
Posts: 5,425
04-12-2008, 04:42 AM
 
Variables in PHP


Variables are probably one of the most commonly used code constructs you’ll come across while programming. If you have any experience of mathematics, especially algebra, you’ll be aware of the concept of using symbols to represent unknown quantities in equations, like in the following example x + 5 = 7;
from this we can deduce that x = 2

In PHP it works in a similar fashion, except we usually know the value of x, and simply assign it a specific value, such as x = 5. We can then use that value to perform calculations using other symbols and numbers. For example
$x = 5 //x now equals 5
$y = $x + 10 /* y now equals 5 + 10 */

These symbols in PHP are known as variables and require you to follow a specific syntax to use them properly. Once you understand the principles behind them, you’ll begin to see how they form a major component of any PHP application, and can be used to store many things other than just numbers. Let us begin though, by guiding you through a few simple examples of the use of variables.
A simple introduction



A very simple example of the use of a variable in PHP is to hold just one number, e.g.
$x = 5;

All variables in PHP start with a dollar sign, followed by a string of numbers, letters (both lower-case and capitals) and underscores. Variables are also case sensitive, meaning that $Variable is not equal to $variable. The only other requirement of variable naming is that the first digit cannot be a number. These are examples of valid variable names
PHP Code:
$variable_name = 5; //valid - integer
$VariableName = 25.5; //valid - floating point
$variable3 = 155; //valid
$_variable_number_four = "value"; //valid - string
$_x = "five"; //valid


These are not valid variable names
PHP Code:
$1st_variable = 5; //invalid
$Variable.name = 25; //invalid, cannot have dots in names
</span>

Variables are assigned values through the use of the equals sign (=) operator. Once a variable has been assigned to a value, that variable symbol can be used in place of the value anywhere in the code. The value of the variable can also be changed at any point during the execution of the code. The same value can also be assigned to multiple variables at the same time, like so
PHP Code:
$a = $b = $c = 5;
echo
"$a, $b, $c";


This produces the output
5, 5, 5

Flexible Types



The “type” of a variable is defined by the value you’ve assigned to it. PHP supports a wide range of types, including strings, integers and floats.
PHP Code:
$string = "This is a string";
$integer = 5; //an integer value;
$float = 4.55; //a "floating point" value


PHP doesn’t explicitly require that you maintain the same type for a certain variable though. If you tried to assign a string value to a variable which previously contained an integer, you’ll encounter no problems. Similarly with any other types, floating point variables can hold integers or strings and so on.
__________________
I Love Walking In The Rain Cuz Nobody Know I'm Crying !!
 
 
 
Reply

Classes & Source Code Thread, Variables in PHP in PHP; Variables in PHP Variables are probably one of the most commonly used code constructs you’ll come across while programming. If ...

Short Link: http://forum.egypt.com/enforum/showthread.php?t=6989


Bookmarks

Tags
php, variables


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Template Editing - All the variables and coding bits you need to know! Developer Articles 0 10-09-2008 07:16 PM
Variables. Data Types. Developer Classes & Source Code 0 04-09-2008 10:15 PM
vBulletin Variables List Developer Articles 0 27-08-2008 08:50 PM