Beginners PHP Tutorial – Variables and Operators

November 7, 2008

For users who prefer to learn PHP visually we have a range of video tutorials, this method of training greatly enhances learning and allows beginners to master PHP at their own pace.
View the Beginners PHP Tutorial Videos

.

Beginners PHP Tutorials – Variables and Operators

This short beginners PHP tutorial will give you a basic understand of variables and operators used in PHP and are an essential part of PHP Programming.

Unless you are going to create a script that does nothing more than echo “Hello World!,” it”s unlikely you are going to be able to get around using variables and operators. They are the basic building blocks of any computer program. But don”t worry, they”re a snap to use once you understand these basics:

Variables are used to hold data. That data is called a value. PHP uses these six different variable types:

• Integer (stores whole numbers)
• Double (holds real numbers)
• String (holds text or numbers depicted as text)
• Boolean (designates True or False)
• Array (holds multiple variables and values)
• Object (too advanced for this article)

Variables do not need to be declared in advance when programming in PHP. Many programmers do so anyway, but it’s a personal choice.

The easiest way to assign a value in PHP is to simply do this:

• $R=”100″ // this is a text depiction of a number
• $X=100; // this is an integer
• $Y=.0221 // this is a double
• $Z=”Hello”; // this is a string
• $AString =”World”; // this is another string
• $answer = TRUE; //this is a logical. You can use TRUE,FALSE, or 0 to depict false and 1 to depict true.

Notice that each variable begins with a $. This is how you alert PHP to the fact that you are creating a variable. Numeric variables do not use a quotation mark, text variables and numerics used as text, do.

Variables do not have to be a single letter. You can use any words as well. $MyName=”Joe”;

Text variables usually use a single quote unless the variable contains a variable. Here”s an example.

<?php

$z=”Hello”;
$w=”World”;
$ThisVariable = “I would like to say $z $w”;
echo “$ThisVariable”;

?>
would display: “I would like to say Hello World”

Variables can be added to any spot in a string either by using the above example or by using the period as a continuator. Here is an example.

echo “I would like to say ” .$z. ” And I would also like to say ” .$w. “to everyone.”;

Logical variables are typically used within control structures, such as an if statement to branch out to other code if a certain condition is true or false. Here is an example:

<?php
$x = TRUE;
If ($x){echo “True”;}
Else {echo “False”;}
?>

Note: Setting $x =”sometext” or $x = 1; would also evaluate the above code example to echo out “True”.

When testing for a true or false condition, you do not need to use the equality symbol.

if ($x) means “if the value of $x is true”. If you want to test for false, you use the NOT operator, which is the exclamation mark ( ! ).

Here is an example using the NOT Operator:

<?php
$x = TRUE;
If (!$x){echo “False”;}
Else {echo “True”;}
?>

Numeric variables can be used in any math formula. Here are some examples:

$MyTotal = $x+$y;

$Remaining = $x-$y;

$Piece = $x/$y; //($x divided by $y)

Understanding Operators

Operators go hand in hand with variables. PHP has five categories of operators:

• Assignment Operators
• Math Operators
• Comparison Operators
• String Operators
• Combination of Math and Assignment Operators
Assignment Operator

The assignment operator, equal sign, is used to assign a value to a variable.

$x=15;

Math Operators

Math or Arithmetic operators are used to perform math functions.

 + Addition
- Subtraction
* Multiplication
/ Division
 % Modulus

Comparison Operators
Comparison operators are used to test the relationship between variables or values.

 == Equal To
 != Not Equal To
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To

Combination Operators
Combination operators are primarily as shortcuts for incrementing or decrementing variables in one step.

For example, you might write $x = $x+1 if you were creating a counter. A combination operator would let you write $x+=1 and get the same result, you could also use a shortcut of $X++ , which increments the value of $X by one, $X—subtracts 1 from the value of $X, it”s hard to see just where this would fit in, but once you start using loops and conditional logic you will see how this little count makes life so much easier, in fact let me give you a quick example, lets say we want to print out the 12 times table:
<?php
$count=1;
  while($count <13 ){
    $val=$count*12;
 echo”$count x 12 = $val<br>”;
$count++;
}

?>

As long as the value of count is less than 13 this script will run, at the end of the loop count is increased by 1 and the loop starts again, the minute count is not less than 13 the loop stops executing, if you removed the counter from the bottom of this script the code would execute indefinitely, are at least till your server crashed.

PHP Operators

 +=Plus Equals
-=Minus Equals
*=Multiply Equals
 /=Divide Equals 
%=Modulo Equals $x
.=Concatenate Equals

Concatenate Equals is used with strings. $string = $string.” World.” Could be shortened to $string.=” World.”

Once you start working with variables and operators for a little while, they’ll become second nature to you.


Entry Filed under: PHP. Tags: .

1 Comment Add your own

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Follow us on Twitter

Categories