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);
?>
-----

Sunday, May 4, 2014

PHP Eclipse 102: Variables

-----
PHP Basic 102 Using Eclipse IDE - Variables

INTRODUCTIONS

This tutorial is adapted from: http://www.w3schools.com/PHP/php_variables.asp

STEPS

0) Preparation

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

1) Using Variables (Declare and Display).

As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y).
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case sensitive ($y and $Y are two different variables)
PHP has no command for declaring a variable. A variable is created the moment you first assign a value to it.

usingvariables.php
<?php
//declare variables
$txt="Hello world!";
$x=5;
$y=10.5;
//display variables
echo $txt."<br/>";
echo $x."<br/>";
echo $y."<br/>";
?>

2) Variable Scopes (Local and Global)

In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
  • local
  • global
  • static
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

 variablescopes.php
<?php
$x=5; // global scope
function myTest() {
  $y=10; // local scope
  echo "<p>Test variables inside the function:</p>";
  echo "Variable x is: $x";
  echo "<br>";
  echo "Variable y is: $y";
}
myTest();
echo "<p>Test variables outside the function:</p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>

3) Global Keyword

The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function).

 globalkeyword.php
<?php
$x=5;
$y=10;
function myTest() {
  global $x,$y;
  $y=$x+$y;
}
myTest();
echo $y; // outputs 15
?>

4) GLOBALS Array

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

globals.php
<?php
$x=5;
$y=10;
function myTest() {
  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>

5) Static Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable.

statickeyword.php
<?php
function myTest() {
  static $x=0;
  echo $x;
  $x++;
}
myTest();
myTest();
myTest();
?>
-----

PHP Eclipse 101: Getting Started

-----
PHP Basic 101 Using Eclipse IDE (Hello World)

INTRODUCTION

This tutorial is adapted from http://www.w3schools.com/php/php_syntax.asp .

STEPS

1) Create a new project basic101.

If the item “PHP Project” doesn’t appear in the menu, select “Other…” and select “PHP Project” in the provided list.

2) Creating PHP Hello World program.

2.1) Right-click the project name and select New/PHP File.
2.2) Type the following codes:

helloworld.php
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
black color= HTML codes.
red color= PHP codes.
2.3) Right-click the file helloworld.php, select Run As/PHP Web Application.
(If you have a problem of executing this script file on a web server, follow this this tutorial, http://php-steps.blogspot.com/2014/05/php-program-development-using-eclipse.html )

3) Creating PHP Comments.


comments.php
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single line comment
# This is also a single line comment
/*
This is a multiple lines comment block
that spans over more than
one line
*/
?>
</body>
</html>

4) PHP Code Case Sensitivities.


codecase.php
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>




4) PHP Variable Case Sensitivities


variablecase.php
<!DOCTYPE html>
<html>
<body>
<?php
$color="red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
You should get a warning message “Undefined variable” for COLOR and coLOR variables.
To turn off the error message, go to UwAmp Control Panel, click PHP Config, click PHP Setting, click Display Errors item and change the value to Off.
Refresh your browser.
Since we are now in the development stage, set the Display Errors value back to On so that the server could tell you the problems in your codes.
-----

Labels