Showing posts with label My PHP Notes. Show all posts
Showing posts with label My PHP Notes. Show all posts

Friday, August 28, 2009

PHP Data Types


Bookmark and Share


Two Data Type Categories


  1. Scalar - contains only one value at a time

  2. Composite or Compound - array and objects

Four scalar types supported


  1. boolean - a value that can only either be true or false

  2. int - a signed numeric integer value

  3. float - a signed floating-point value

  4. string - a collection of binary data

Numeric Values


PHP recognizes two types of numbers


  1. integers
    - the int data type is used to represent signed integers

  2. floating-point values
    - also called float and sometimes double, are numbers that have a fractional component

Numeric Notations


  1. Decimal - standard decimal notation.
    ex. 10; -11; 1452
    Note: no thousand separator is needed or allowed

  2. Octal - identified by its leading zero (like the access permission of Unix)
    ex. 0666, 0100

  3. Hexadecimal
    ex. 0x123, 0XFF; -0x100

Note
The leading 0x prefix are both case-insensitive
Note
Octal numbers can easily confused with decimal numbers and can lead to some consequences

Two supported notations for Floating-point numbers


  1. Decimal - traditional decimal notation
    ex. 0.12; 1234.43; -.123

  2. Exponential - composed of a mantissa value followed by a case-insensitive letter E then an exponent value.
    ex. 2E7, 1.2e2

Note
  1. The precision and range of both types varies depending on the platform on which your scripts run
    ex. 64-bit platforms may be capable of representing a wider range of integer numbers than 32-bit platforms

  2. PHP doesn't tract overflows

  3. Float data type is not always capable of representing numbers in the way you expect it to.
    ex. echo (int)((0.1 + 0.7) * 10);
    this results to 7 instead of 8 since the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8.0.

Strings


An ordered collection of binary data.
  • could be text, content of an image file, spreadsheed or even a music recording.

Booleans


  • can only contain either TRUE or FALSE.

  • used as basis for logical operations.

Note
When converting data to and from the Boolean type, several rules apply:
  1. When a numeric variable is converted to boolean data type, the value becomes false if the variable contains zero and true otherwise.

  2. When a string variable is converted to boolean data type, the value becomes false only when the variable is empty or if it contains a single character 0. Other values, it is converted to true.

  3. When a boolean variable is converted to a numeric or string, it becomes 1 if it is true, and 0 otherwise.


Bookmark and Share

Friday, August 7, 2009

Anatomy of a PHP Script


Bookmark and Share

  • every PHP script is made up of statements.

  • each statement must be terminated with a semicolon.
    Example:

    some_instruction();
    $variable = 'value';



Note:

The last instruction before a closing tag does not require a semicolon, but it is advisable to always terminate an instruction with a semicolon.

Comments


Types of PHP comments:
  1.  // Single line comment

  2.  # Single line comment

  3.  /* Multi-line
    comment
    */

  4.  /**
    * API Documentation Example
    *
    * @param string $bar
    */
    function foo($bar) { }


Note:

Because the closing tag ?> will also end a comment, code like:
// Do not show this ?> or this 

will output:
or this

  • because the single line comment was terminated by ?>

  • Normally, a single line comment should be terminated with a newline (\r, \n or \r\n)



Whitespace


PHP is whitespace-insentive, except with this few limitations:
  1. You cannot have any whitesapce between <? and php

  2. You cannot break apart keywords
    ex. whi le, fo r, and funct ion

  3. You cannot break apart variable names and function names
    ex. $var name and function foo bar()


Code Block


A series of statements enclosed between two braces:
{
// some comments
f(); // a function call
}

  • used in creating groups of script lines that must all be executed under specific circumstances, such as a function call or a conditional statement.

  • code blocks can be nested.


Language Constructs


Elements that are built-into the language that follow specific rules.
Ex.

echo 10; // will output 10
print (10); // will also output 10
exit(); // terminate the script and either output a string or return a numeric status
die(); // an alias of exit()

Note:

echo is not a function and therefore does not have a return value

Bookmark and Share

Thursday, August 6, 2009

PHP Syntax


Bookmark and Share




  • simple and easy to understand.

  • derived from many languages but most of it were from C and some from Perl.

  • its object-oriented syntax were patterned from Java and C++.


Source Files and 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.



4 Types of Tags



  1. Standard Tags

    <?php
    ... code
    ?>


  2. Short Tags

    <?
    ... code
    ?>
    <?= $variable ?>


  3. Script Tags

    <script language="php">
    ... code
    </script>


  4. ASP Tags

    <%
    ... code
    %>



Note:

3 and 4 are all considered deprecated.

Note:

To prevent spurious output from an include file, omit the closing tag at the end

Bookmark and Share