Basic PHP Data Type – Booleans

June 6, 2010

Booleans : This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE. Booleans are used as the basis for logical operations.

Syntax :-

To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.

<?php
$isBool = True; // assign the value TRUE to $foo
?>

Converting to boolean :-

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unncecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

When converting data to and from the Boolean type, several special rules apply:

  1. the boolean FALSE itself
  2. the integer 0 (zero)
  3. the float 0.0 (zero)
  4. the empty string, and the string “0″
  5. an array with zero elements
  6. an object with zero member variables (PHP 4 only)
  7. the special type NULL (including unset variables)
  8. SimpleXML objects created from empty tags
  9. Every other value is considered TRUE (including any resource)
  10. -1 is considered TRUE, like any other non-zero (whether negative or positive) number!

<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)
?>


Basic PHP Data Type – Introduction

February 18, 2010

Introduction :

PHP supports eight primitive types.

Four scalar types: 1) boolean, 2) integer, 3) float, 4) string

Two compound types: 1) array, 2) object

Two  special types: 1) resource, 2) NULL

Pseudo-types: 1) mixed, 2) number, 3) callback

To check the type and value of an expression, use the var_dump() function. To get a human-readable representation of a type for debugging, use the gettype() function. To check for a certain type, do not use gettype(), but rather the is_type functions.

PHP takes care of converting between data types transparently when a datum is used in an expression. However, it is still possible to force the conversionof a value to a specific type using type conversion operators. These are simply the names of the data type you want to convert to enclosed in brackets and placed before an expression. For example:

$x = 5.55;
echo (int) $x; // Outputs 5

Note that a value cannot be converted to some special types; for example, you cannot convert any value to a resource—you can, however, convert a resource to a numeric or string data type, in which case PHP will return the numeric ID of the resource, or the string Resource id # followed by the resource ID.


Basic PHP Syntax

February 16, 2010

PHP Tags :

PHP code can be inserted directly into a text file using a special set of tags; the interpreter will then output any text outside the tags as-is, and execute the code that is between the tags.

There are four types of tags available:

  1. Standard Tags :- <?php … code ?>
  2. Short Tags :- <? … code ?> or <?= $variable ?>
  3. Script Tags :- <script language=“php”> … code </script>
  4. ASP Tags :- <% … code %>

Short tags and ASP style tags can be turned on and off from the php.ini configuration file. Short tags, script tags and ASP tags are all considered deprecated and their use is strongly discouraged.

Instruction separation :

PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require().

Comments :

PHP supports ‘C’, ‘C++’ and Unix shell-style (Perl style) comments.

  1. // Single line comment
  2. # Single line comment
  3. /* Multi-line
    comment
    */
  4. /**
    * API Documentation Example
    *
    * @param string $someStr
    */

Both types of single line comments, // and #, can be ended using a newline (\r, \n or\r\n) or by ending the current PHP block using the PHP closing tag—?>.

Language Constructs :

Constructs are elements that are built-into the language and, therefore, follow special rules. Perhaps the most common of them is the echo statement, which allows you to write data to the script’s output:

echo 10; // will output 10

It’s important to understand that echo is not a function and, as such, it does not have a return value. If you need to output data through a function, you can use print() instead:

echo 10;
print (10);

Another very important construct is die(), which is itself an alias of exit(). It allows you to terminate the script’s output and either output a string or return a numeric status to the process that called the script.


Something about PHP

February 14, 2010

Something about php from PHP manual and W3schools:

  1. PHP, which stands for “PHP: Hypertext Preprocessor”.
  2. Widely-used Free Open Source general-purpose scripting language.
  3. Server side scripting language (scripts are are executed on the server), especially suited for Web development and can be embedded into HTML.
  4. Supports MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc many databases.
  5. Runs on different platforms (Windows, Linux, Unix, etc.)
  6. Compatible with almost all servers used today (Apache, IIS, etc.)
  7. Easy to learn and runs efficiently on the server side.

Zend And W3Schools PHP Certification Exam

February 13, 2010

I am trying to take preparation for both “Zend PHP 5 Certification” and “W3Schools PHP Certification “. I will study below documents for first time:

  1. PHP Manual
  2. php|architect’s Zend PHP 5 Certification Study Guide
  3. w3schools

And i will also try to note here everything what i will learn. I am too much busy with my job so will spend about one hour before go to bed at night.

(Insha Allah)


Follow

Get every new post delivered to your Inbox.