Test

Powered by Blogger.

Sunday 22 April 2012

php datatypes

PHP consists of 4 basic data types:
boolean - a boolean is actually much like an integer, but with only two possible values: 0 or 1, which is either false or true.

integer - a number with no decimals, e.g 3 or 42.

float (sometimes referred to as double) - a number that may include decimals, e.g. 42.3 or 10.9.

string - A number of characters, which combined makes a text string.

Besides that, there are a couple of more complex datatypes:

array - holds an array of items, e.g. several strings or several integers. An array may contain variables which are arrays which are arrays and so on.

object - a reference to an instance of a class. This is related to Object Oriented programming, which we will talk more about later on in this tutorial.

There are also a couple of special types:

resource - holds a reference to a special external resource. It may be a file resource, or perhaps an open database connection.

NULL - a value of null is nothing. It's not the same as 0 (zero), because that's actually a value. Null is truly nothing. Variables which have not yet been assigned a value, or which you have used the unset() method on, will carry the NULL value. This is useful if you wish to check whether or not a variable contains any value - you may compare it against the NULL constant.

In the next couple of chapters, we will look into working with both strings and numbers (integers and floats), and later on, we will look into both arrays and objects.


In this chapter, we will work with some numbers. As mentioned in a previous chapter, PHP have two different data types related to numbers: Integers and floats. Integers are whole numbers, without a decimal separator, while floats always carry a fractional part as well. In most cases, integers will be sufficient, and they are faster and simpler to work with.

Let's try some simple calculations, to show you how easy it is to do math with PHP. Here are a couple of examples.

<?php
$number1 = 6;
$number2 = 2;

echo "Addition: " . ($number1 + $number2);
echo "<br /><br />";
echo "Substraction: " . ($number1 - $number2);
echo "<br /><br />";
echo "Multiplication: " . ($number1 * $number2);
echo "<br /><br />";
echo "Division: " . ($number1 / $number2);
echo "<br /><br />";
?>


Now that's simple! Basic math with PHP, and it looks kinda like using a simple calculator. But what about numbers which comes from the outside world, e.g. from a submitted form? We will dig into in a later chapter, and one of the techniques we will be using, is checking if a variable holds a value that we can use for math. The is_numberic() function will help us with this. It checks whether a variable is an integer, or if it can be converted to one. For instance, "42" can be converted to 42, but "Hello World", well, that's a tough one. is_numberic will return true in the first case and false in the second case. Here is an example:

<?php
$number1 = "10";
$number2 = 20;

if((is_numeric($number1)) && (is_numeric($number2)))
    echo "Result: " . ($number1 + $number2);
else
    echo "Both variables have to be numbers!";
?>

Now, try changing "10" to a word or a sentence, e.g. "Hello world". You will see that the condition fails, and our warning will be printed. This comes in handy when validating user input, as we will see later on.

In some situations, you may need a real integer instead of a string that is numeric, like in the above example. Even though PHP is loosely typed, type casting does exist - you may cast one type to another. Have a look at this example:

<?php
$var = "30";
echo "The current datatype of var is: " . gettype($var) . "<br /><br />";
$var = (int)$var;
echo "The current datatype of var is: " . gettype($var);
?>

We use the int keyword, inside a set of parentheses, to define a new type for the variable. Obviously, it works the other way around as well - you may cast an integer to become a string and so on.
Floats
As mentioned, there are two different data types for working with numbers: Integers and floats. In the next example, I will try to show you the difference. Try running this piece of sample code:

<?php
$number1 = 10;
$number2 = 3;
$number3 = 5;

$result = $number1 / $number2;

echo "Result as a float: " . $result . "<br /><br />";
echo "Result as a float, rounded: " . round($result, 2) . "<br /><br />";
echo "Result as an integer: " . (int)$result . "<br /><br />";
echo "Multiplication as a float: " . ($result * $number3) . "<br /><br />";
echo "Multiplication as an integer: " . ((int)$result * $number3) . "<br /><br />";
?>

No comments:

Post a Comment

RSS

Categories

Followers

Blog Archive

Sunday 22 April 2012

php datatypes

PHP consists of 4 basic data types:
boolean - a boolean is actually much like an integer, but with only two possible values: 0 or 1, which is either false or true.

integer - a number with no decimals, e.g 3 or 42.

float (sometimes referred to as double) - a number that may include decimals, e.g. 42.3 or 10.9.

string - A number of characters, which combined makes a text string.

Besides that, there are a couple of more complex datatypes:

array - holds an array of items, e.g. several strings or several integers. An array may contain variables which are arrays which are arrays and so on.

object - a reference to an instance of a class. This is related to Object Oriented programming, which we will talk more about later on in this tutorial.

There are also a couple of special types:

resource - holds a reference to a special external resource. It may be a file resource, or perhaps an open database connection.

NULL - a value of null is nothing. It's not the same as 0 (zero), because that's actually a value. Null is truly nothing. Variables which have not yet been assigned a value, or which you have used the unset() method on, will carry the NULL value. This is useful if you wish to check whether or not a variable contains any value - you may compare it against the NULL constant.

In the next couple of chapters, we will look into working with both strings and numbers (integers and floats), and later on, we will look into both arrays and objects.


In this chapter, we will work with some numbers. As mentioned in a previous chapter, PHP have two different data types related to numbers: Integers and floats. Integers are whole numbers, without a decimal separator, while floats always carry a fractional part as well. In most cases, integers will be sufficient, and they are faster and simpler to work with.

Let's try some simple calculations, to show you how easy it is to do math with PHP. Here are a couple of examples.

<?php
$number1 = 6;
$number2 = 2;

echo "Addition: " . ($number1 + $number2);
echo "<br /><br />";
echo "Substraction: " . ($number1 - $number2);
echo "<br /><br />";
echo "Multiplication: " . ($number1 * $number2);
echo "<br /><br />";
echo "Division: " . ($number1 / $number2);
echo "<br /><br />";
?>


Now that's simple! Basic math with PHP, and it looks kinda like using a simple calculator. But what about numbers which comes from the outside world, e.g. from a submitted form? We will dig into in a later chapter, and one of the techniques we will be using, is checking if a variable holds a value that we can use for math. The is_numberic() function will help us with this. It checks whether a variable is an integer, or if it can be converted to one. For instance, "42" can be converted to 42, but "Hello World", well, that's a tough one. is_numberic will return true in the first case and false in the second case. Here is an example:

<?php
$number1 = "10";
$number2 = 20;

if((is_numeric($number1)) && (is_numeric($number2)))
    echo "Result: " . ($number1 + $number2);
else
    echo "Both variables have to be numbers!";
?>

Now, try changing "10" to a word or a sentence, e.g. "Hello world". You will see that the condition fails, and our warning will be printed. This comes in handy when validating user input, as we will see later on.

In some situations, you may need a real integer instead of a string that is numeric, like in the above example. Even though PHP is loosely typed, type casting does exist - you may cast one type to another. Have a look at this example:

<?php
$var = "30";
echo "The current datatype of var is: " . gettype($var) . "<br /><br />";
$var = (int)$var;
echo "The current datatype of var is: " . gettype($var);
?>

We use the int keyword, inside a set of parentheses, to define a new type for the variable. Obviously, it works the other way around as well - you may cast an integer to become a string and so on.
Floats
As mentioned, there are two different data types for working with numbers: Integers and floats. In the next example, I will try to show you the difference. Try running this piece of sample code:

<?php
$number1 = 10;
$number2 = 3;
$number3 = 5;

$result = $number1 / $number2;

echo "Result as a float: " . $result . "<br /><br />";
echo "Result as a float, rounded: " . round($result, 2) . "<br /><br />";
echo "Result as an integer: " . (int)$result . "<br /><br />";
echo "Multiplication as a float: " . ($result * $number3) . "<br /><br />";
echo "Multiplication as an integer: " . ((int)$result * $number3) . "<br /><br />";
?>

No comments:

Post a Comment