Archive for November 3, 2010

Learn PHP:15-Arithmetic Operators in PHP

Posted: November 3, 2010 in PHP
Tags:

PHP can perform simple mathematical operations all the way to complex trigonometric equations. The operator symbols used all make good sense for the mathematical action they perform.

Here are the arithmetic symbols and how to apply them in the most basic way:

<?php
// Set a couple of sample integer variables
$var1 = 5;
$var2 = 3;
// Addition >>> Sum of $var1 and $var2
echo $var1 + $var2;
echo "<br />";
// Subtraction >>> Difference of $var1 and $var2
echo $var1 - $var2;
echo "<br />";
// Multiplication >>> Product of $var1 and $var2
echo $var1 * $var2;
echo "<br />";
// Division >>> Quotient of $var1 and $var2
echo $var1 / $var2;
echo "<br />";
//Modulus >>> Remainder of $var1 divided by $var2
echo $var1 % $var2;
echo "<br />";
//Negation >>> Opposite of $var1
echo -$var1;
?>

The output is shown below :

8
2
15
1.66666666667
2
-5

Learn PHP:14-The Assignment Operator

Posted: November 3, 2010 in PHP
Tags:

The Assignment operator is the most widely used and well known of all the operators in PHP. You use it every time you create a variable. The “=”(equal to) sign is used, and what it does is it takes the expression from the right and places it into the operand on the left. It is simple.

In this example we use the assignment operator(=) to place a value of 8 into the variable:

 <?php
$var1 = 8;
?>

We can also combine operators together to produce expression results that are set into our variables as value. We demonstrate this in the next few lessons quite well. Let us continue on.

Learn PHP:13-Understanding Expressions in PHP

Posted: November 3, 2010 in PHP
Tags:

PHP is an expression-oriented language, in the sense that almost everything is an expression. An expression in PHP is anything that can be used as a value. Including integer constants, variables, and function calls. It is any combination of values, variables, operands, and functions that sets a value. If it can be used as if it were a value, it is an expression in PHP. When we take a variable and use an operator on it or a pair of variables we have created an expression in PHP. PHP expressions are best defined as “anything that has a value”.

When we create simple variables we are using expressions: 

<?php
$var1 = 10; // 10 is an expression with an integer type value of 10
$var2 = $var1; // $var1 is an expression with an integer type value of 10
?>

 

Learn PHP:12-Changing Data Types

Posted: November 3, 2010 in PHP
Tags:

PHP has loose data typing. Because of this, changing a variable’s data type is not a commonly used mechanism in PHP due to the fact that PHP will automatically cast your variable types when your script changes to use the variable as a different type and in a different way.

As illustrated in this sample code:

 <?php
$var1 = "18 years old";
$var2 = "14 years old";
// Since PHP knows that it cannot add two strings mathematically it automatically
// removes the string parts of the variables and proceeds to do the math correctly
$sumOfBoth = $var1 + $var2;
// Very interesting that we get such a result and not an error
echo $sumOfBoth;
?>

(more…)

PHP sports a cool feature called automatic data typing. A PHP developer can claim variables and use them in most common situations without having to claim the data type that the variable is. But this is also a double edged sword because as a developer gets more advanced they may create functions or scripts that only accept one data type, and throw errors if you feed it a different type. But that is usually only in the most complex of PHP applications.

For instance, if my imported script and function is expecting an integer type variable and I feed it a string type, I will get an error and know that I must not try to feed it a string… it needs an integer type variable. No big deal, I will just make sure a number type variable is all that gets sent through that mechanism.

Here are the data types:
Boolean, Integer, Floating Point Number, String, Array, Object, Resource, Null

<?php
// Boolean... true or false data types
$userChoice = true;

// Integer.... whole numbers
$num1 = 3;

// Floating Point Numbers
$num2 = 8.357

// String... a set of alphanumeric characters
$string1 = "Hello World, I just turned 21!";

// Array... we cover building arrays in later lessons
$my_friend_array = array(1 => 'Joe', 2 => 'Adam', 3 => 'Brian', 4 => 'Susan', 5 => 'Amy', 6 => '0', 7 => 'Bob');

// Object...
// Objects work with classes, which we will cover later

// Resource...
// A special variable, holding a reference to an external resource.
// Resources are created and used by special functions.

// Null... value for missing, empty, or unset variables. If null... it has no value and is not set.
$var1 = NULL; // Casting a variable to null will remove the variable and unset its value
?>



(more…)