Friday, May 2, 2014

PHP Variables

PHP - Variables

If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you.
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
  • $variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

PHP Code:

<?php

$x=5;

$y=6;

$z=$x+$y;

echo $z;

?> 
 
Output:11

Declaring PHP Variables:

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Code: 

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
echo $txt;
echo $x;
echo $y;
?> 
Output:
Hello world!
5
10.5 

  Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case sensitive ($y and $Y are two different variables)
 
 
 

 


No comments:

Post a Comment