Advertising


Navigation
External Links
Banner

What's a Variable?

Taken middle school algebra before? Yes? Cool. We're done here then. Next.

Well, okay, it's basically just the same thing. It's just like solving for x. Here's a tad of code, let's see if you can figure out what happens.

    <?PHP
    $i = '4';
    $x = '1';
    echo $i-$x;
    ?>


What's the output there? If you said anything besides '3' stop reading this. Go back to school. Kidding aside. That's all a variable. Although I should note that in PHP a variable is denoted by a "$" sign. PHP makes it easy for you, unlike other programming languages you don't even have to define the variable to use it! Nope. In fact, you don't even have to define a type of variable. You just sort of use it and it just sort of works. There's more details, but it's actually not important at all.


This also works for other things to like strings. Here, let's try it:

    <?PHP
    $x = "Ninja's kill Pirates";
    echo $x;
    ?>

That will output "Ninja's kill Pirates" That's basically it!



1+1


Let's do some math. Any programming language should be able to do math and PHP of course, can do just that.

You should have seen most of these before but let's review:

Addition ( + )
The addition operator adds the two numeric operands OR, it concatenates, which is fancy lingo for "puts together" two string operands. Values need to be convertible to a number to work with the addition operator.
Subtraction ( - )
The subtraction operator subtracts the value of the second operand (the one after the minus sign) from the value of the first operand (the one before the minus sign). It only works on numbers.
Multiplication ( * )
The multiplication operator multiplies the numeric values of two operands together.
Division ( / )
The division operator divides its first operand by its second operand. Division of integers that do not divide evenly will yield a floating-point result. Division by zero yields Infinity or -Infinity. The expression 0/0 evaluates to Nan.
Modulo ( % )
The modulo operator divides its first operand by its second and returns the modulus. The modulus in division is the remainder, or what is left over.
Unary Minus ( - )
The unary minus is a unary operator that negates the operand immediately following it, which is to say, converts it to its negative equivalent.
Unary Plus ( + )
This operand does nothing, but exists to balance the unary minus. Stupid? Sure.

Here are a few cool operaters you may not know of:

Increment ( ++ )
The increment operator increments the numeric value of its operand by one. If placed before the operand, it returns the incremented value. If placed after the operand, it returns the original value and then increments the operand.
Decrement ( -- )
The decrement operator decrements the numeric value of its operand by one. In other words, it counts backwards. If placed before the operand, it returns the decremented value. If placed after the operand, it returns the original value and then decrements the operand.

So what's the mean? Well, let's put it together in a little example

    <?PHP
      $i = '1';
   echo $i++;
    ?>

This code will return '2'. PHP is fairly interesting because unlike other langauges it allows you to increment strings also. like so:

    <?PHP
      $i = 'a';
   echo $i++;
    ?>


Now this code will  return 'b'. Interesting no?


Page 2 of 6

Previous 1 2 3 4 5 6 Next