Monday, May 5, 2014

PHP Eclipse 103: Data Types

-----
PHP Eclipse 103: Data Types
This tutorial is adapted from: http://www.w3schools.com/PHP/php_datatypes.asp

STEPS

0) PREPARATION

Create a subfolder in your project and name it as 103.

1) PHP Strings

A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
stringvariables.php
<?php
        $x = "Hello world!";
        echo $x;
        echo "<br>";
        $x = 'Hello world!';
        echo $x;
?>
1.0) Start with PHP Perspective. Create new PHP File.
1.1) Edit the codes. (Make sure that you are using PHP Perspective)
1.2) Run as PHP Web Application.
1.3) Accept default Launch URL.
1.4) View in Internal Browser.

2) PHP Integers

An integer is a number without decimals.
Rules for integers:
  • An integer must have at least one digit (0-9)
  • An integer cannot contain comma or blanks
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:
integervariables.php
<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; // negative number
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x);
?>

3) PHP Floating Point Numbers

A floating point number is a number with a decimal point or a number in exponential form.
In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:
floatvariables.php
<?php
$x = 10.365;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>

4) PHP Booleans

Booleans can be either TRUE or FALSE.
booleanvariables.php
<?php
$x=true;
$y=false;
var_dump($x);
var_dump($y);
?>

5) PHP Arrays

An array stores multiple values in one single variable.
In the following example we create an array, and then use the PHP var_dump() function to return the data type and value of the array:
arrayvariables.php
<?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
?>

6) PHP Objects

An object is a data type which stores data and information on how to process that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.
We then define the data type in the object class, and then we use the data type in instances of that class:
objects.php
<?php
class Car
{
        var $color;
        function Car($color="green") {
          $this->color = $color;
        }
        function what_color() {
          return $this->color;
        }
}
function print_vars($obj) {
   foreach (get_object_vars($obj) as $prop => $val) {
         echo "\t$prop = $val\n";
   }
}
// instantiate one object
$herbie = new Car("white");
// show herbie properties
echo "\t herbie: Properties\n";
print_vars($herbie);
?>  

6) PHP NULL Value

The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.
The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.
Variables can be emptied by setting the value to NULL:
nullvalue.php
<?php
$x="Hello world!";
$x=null;
var_dump($x);
?>
-----

No comments:

Post a Comment

Labels