Monday, August 20, 2012

PHP Scalar Data Types

A data type identifies the characteristics that the assigned data will be interpreted by when interacting with
the PHP programming language. When a value is assigned a specific data type, the PHP interpreter will
work with the data based on the expected type of data it is. For example, 27654 could be considered a
numeric data type or it could be considered a string data type, such as in the zip code for a customer. Even
though you could perform mathematical equations on the zip code, it would make no sense to do so. Thus,
zip codes, even though they look like they would be numeric numbers should be identified as a string data
type to eliminate problems such as zip codes that start with zero (ex. 08102). Assigning the correct data
type to the expected value is an important part of working with PHP. There are three categories of data
types in PHP: Scalar, Compound and Special.



Scalar Data Types
A scalar data type is identified as being able to hold
one value at a time. In PHP, there are four data
types that fall into this category.

• Boolean - A boolean value is a truth value, either 'true' or 'false', often coded 1 and 0, respectively.
When a value is assigned the Boolean data type, there are only two responses that the value can be
assigned: True (1) or False (0). If a value is assigned anything other than these two values, it will
default to True (1).
 <?php  
 $Wealthy = false; // $Wealthy is false  
 $Wealthy = 1; // $Wealthy is true  
 $Wealthy = 5; // $Wealthy is true  
 $Wealthy = -1; // $Wealthy is true  
 $Wealthy = 0; // $Wealthy is false  
 ?>  
A Boolean Value is either TRUE or FALSE typed in either upper or lower case and should not be in quotes.
Try the following program:

 <?php  
   $var0 = true;  
   $var1 = false;  
   $var2 = TRUE;  
   $var3 = FALSE;  
   echo $var0."<br />";  
   echo $var1."<br />";  
   echo $var2."<br />";  
   echo $var3."<br />";  
 ?>  

If you tried the above program, you would have noticed that no value was displayed for false; do not worry about that.Boolean values are usually the result of operators in conditionals like the if-condition. Read and try the following program:

 <?php  
   if (true)  
     {  
       echo "It is true indeed."."<br />";  
     }  
   if (5==5)  
     {  
       echo "5==5 as a condition with the operator, ==, returns TRUE."."<br />";  
     }  
 ?>  
TRUE or FALSE are Boolean literals. There are other literals that are equivalent to TRUE or FALSE. That is what we look at in this section.

The following returned values are equivalent to FALSE:

- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string (""), and the string "0"
- an array with zero elements
- the special type NULL (including unset variables) – See later
- SimpleXML objects created from empty tags – See later

The following returned values are equivalent to TRUE:
- Any literal or data type that is not in the above list is equivalent to TRUE.
--------------------------------------------------------------------------------------------------------------
• Integer - An integer is a whole numeric data type (meaning it does not contain any fractions) that
can be set to decimal (base 10), octal (base 8) or hexadecimal (base 16). The following
demonstrate some of the values that can be assigned the numeric data type:
 <?php  
 $Temperature = 22; // $Temperature is an integer  
 $x = -45672; // $x is an integer number  
 $y = 0642; // $y is an integer number (octal)  
 $z = 0x5E5B; // $z is an integer number (hexadecimal)  
 ?>  

The maximum number that can be assigned to an integer is based on the system. 32 bit systems
have a maximum signed integer range of -2147483648 to 2147483647. The maximum signed
integer value for 64 bit systems is 9223372036854775807. Any value that exceeds the maximum
integer size will be assigned the float data type.

• Float - Floating point numbers are any number that has a fractional component or exceed the
integer maximum values. This data type is also referred to as floats, doubles or real numbers.
Floats are used to represent such numbers as monetary values, distances, weights, scientific
notations and a host of other values:
 <?php  
 $TotalCost = 67.25; // $TotalCost is a float  
 $Distance = 4.5e5; // $Distance is a float equal to 450000  
 $Weight = 92.0; // $Weight is a float  
 $Diff = 2.34E+12; // $Diff is a float (2340000000000)  
 ?>  

The size of a floating point number is dependent by the system being used. In addition, floating
point numbers are not as accurate as integer numbers when it comes to precision. Even simple
numbers like 0.2 and 0.17 can not be converted into their binary equivalents without a loss of
precision. With that said, floating point numbers should never be used in comparison operations
without building in logic such as rounding numbers prior to the comparison operation or using a
tolerance value that would be an acceptable difference between the compared values for
comparison operations.

• String - A string data type is a series of characters that are associated with each other in a defined
order. There is no limit to the length of a string data value. The following represent some of the
values that can be assigned to a string data type:

 <?php  
 $Name = 'Jose Vargás'; // $Name is a string  
 $Value = '£45.20'; // $Value is a string  
 $Location = "subway\n"; // Contains a character return  
 $x = "This is a 'test'"; // $x is a string  
 $Chapter = '1.2.4' // $Chapter is a string  
 ?>  


The characters that can be used in PHP string data types are limited to the ISO 8859-1 or Latin-1
character set. This character set allows for 256 (the size of a byte) different possible characters
(191 of them that are actually printable); however, there is support in PHP to encode strings to the
UTF-8 standard (UTF-8 is a standard mechanism used by Unicode for encoding wide character
values into a byte stream).


PHP Array

These are a series of like values that are assigned to an individual variable with each individual value in the array being referenced with a unique identifier. Once these values are stored into an array, it is possible to retrieve individual values or to perform multiple actions against the stored values. The following demonstrates the storing and retrieval of an array set:

An array can be used when we want a single variable to hold multiple values. Values in arrays can be accessed by using the index associated to it. Each element in the array has its own index. Default start of index starts from 0. Arrays are useful when you want to store a group of data.

Array Example 1
 <?php  
 $Country[0] = "Australia"; // Australia is the first(0) value  
 $Country[1] = "Russia"; // Russia is the second(1) value  
 $Country[2] = "Germany"; // Germany is the third(2) value  
 $Country[3] = "France"; // France is the fourth(3) value  
 // The following will display: Let's go to Germany!  
 print "Let's go to $Country[2]!";  
 ?>  
Demo

The above example could be written less explicitly:

 <?php  
 $Country = array("Australia", "Russia", "Germany", "France");  
 print "Let's go to $Country[2]!";  
 ?>  
Demo

Array Example 2

Rather than using numeric identifiers, array identifiers can be more meaningful when created as in
the following examples:
 <?php  
 $Capital["Australia"] = "Canberra";  
 $Capital["Russia"] = "Moscow";  
 $Capital["Germany"] = "Berlin";  
 $Capital["France"] = "Paris";  
 print "The capital of Russia is ".$Capital["Russia"]."!";  
 ?>  
 <?php  
 $Capital = array(  
 "Australia" => "Canberra",  
 "Russia" => "Moscow",  
 "Germany" => "Berlin",  
 "France" => "Paris");  
 print "The capital of Russia is ".$Capital["Russia"]."!";  
 ?>  
Demo

Array Example 3

 <?php   
                // defining a simple array  
                $array1 = array(4,8,15,16,23,42);  
                // referencing an array value by its index  
                echo $array1[0];  
                // arrays can contain a mix of strings, numbers, even other arrays  
                $array2 = array(6,"fox", "dog", array("x", "y", "z"));  
                // referencing an array value that is inside another array  
                echo $array2[3][1];  
           ?>  
           <br />  
           <?php  


Demo

Array Example 4

 <?php   
                // defining a simple array  
                $array1 = array(4,8,15,16,23,42);  
                // referencing an array value by its index  
                echo $array1[0];  
                // arrays can contain a mix of strings, numbers, even other arrays  
                $array2 = array(6,"fox", "dog", array("x", "y", "z"));  
                // referencing an array value that is inside another array  
                echo $array2[3][1];  
           ?>  
           <br />  
           <?php  

Demo

Array Example5

 <?php  
                // You can also assign labels to each pocket (called "keys"),  
                $array3 = array("first_name" => "Kevin", "last_name" => "Skoglund");  
                // which will allow you to use the key to reference the value in that array position.  
                echo $array3["first_name"] . " " . $array3["last_name"] . "<br />";  
                $array3["first_name"] = "Larry";  
                echo $array3["first_name"] . " " . $array3["last_name"] . "<br />";  
           ?>  

Demo

Array Example 6


        <?php  
                // Changing values in an array that has already been defined  
                // It's just like variables but you use the index to reference the array position  
                $array2[3] = "cat";  
                echo $array2[3];  
           ?>  

Demo


 

Wednesday, August 15, 2012

Starring php Basics



  • // single-line comments can be like this
  • # or even like this
  • /* multi-line comments can be like this */
  •  ; The semicolon at the end of the statement is important! 
  • the PHP command echo is a means of outputting text to the web browser

Comparison chart


Echo (PHP)Print (PHP)
Parameters:echo can take more than one parameter when used without parentheses. The syntax is echo expression [, expression[, expression] ... ]. Note that echo ($arg1,$arg2) is invalid.print only takes one parameter.
Return value:echo does not return any valueprint always returns 1 (integer)
Syntax:void echo ( string $arg1 [, string $... ] )int print ( string $arg )
What is it?:In PHP, echo is not a function but a language construct.In PHP, print is not a really function but a language construct. However, it behaves like a function in that it returns a value.

The print() function is slightly slower than echo().

Example:

<html>
<head>
<title>Hello World! PHP</title>
</head>
<body>

<h1>Examples</h1>
<?php echo "Hello World!"; ?>
<br />



<?php
// print works like echo
print "Hello World!";
?>
<br />

<?php
// concatenation
echo "Hello" . " World!<br />";

// simple math
echo 2 + 3;

?>
<br />
</body>
</html>

About php Variables

  • variable is used for storeing information
  • All variables in PHP are denoted with a leading dollar sign ($)
  • PHP does a good job of automatically converting types from one to another when necessary.

Code:
<html>
<head>
<title>About php Variables</title>
</head>
<body>
<?php
$a = "hello";
$hello = "Hello everyone.";
echo $a ."<br />";
echo $hello."<br />";

echo $$a."<br />";



?>
</body>
</html>

phpinfo()

What is phpinfo?

PHPinfo is a function that returns information, in HTML form, about the PHP environment on your server.


phpinfo() outputs plain text instead of HTML when using the CLI mode.


<?php

/* WARNING: NEVER include this file on your production machine

It's very useful for development but it also gives away WAY too much

information about your system if anyone got access to it.

*/

phpinfo();

?>