The Developer Day | Staying Curious

Apr/08

16

Zend PHP certificate study guide overview (part I)

I’m currently preparing to take the Zend PHP certificate exam and have just finished reading the Zend PHP 5 Certification Study Guide. To help myself and other PHP developers to prepare for the exam I thought I could review every Zend PHP5 certification study guide chapter to provide some highlights on things that may not be known to everyone.

Chapter I - PHP basics

  1. Syntax
  2. Anatomy of a PHP script
  3. Data types
  4. Variables
  5. Constants

1.1 Syntax

You must know that PHP syntax is derived from the C language. PHP syntax has been influenced by Perl and JAVA (latest OOP additions).

PHP supports these opening tags: <?php ?>, <?= ?>, <? ?>, <script language=”php”></script>, <% %>. Interestingly no one knows why <% %> were introduced at all :). Short tags, script tags and ASP tags are all considered deprecated and their use is trongly discouraged.

PHP parser strips new lines after ?> closing tag. An easy way to prevent spurious output from an include file is to omit the closing tag at the end, which the parser considers this perfectly legal.

1.2 Anatomy of a PHP script

It is possible to skip the last semicolon in a PHP script though that is considered a parser quirk and should be avoided.

There are three types of comments in PHP: /* */, //, #. A comment can be ended with a newline or the php closing tag ?>

Interesting to know echo is not a function and, as such, it does not have return value. If you need to output data through a function, you can use print() instead.

An important function is die(); which itself is an alias of exit(); You can echo output with these functions by passing a string or return a numeric status to the process that called PHP by passing an integer.

1.3 Data types

PHP is loosely typed, meaning that it will implicitly change the type of a variable as needed, depending on the operation being performed on its value.

All data types in PHP are divided into two categories scalars and composites. Scalars are: ints, strings, floats, booleans. Numbers can be declared using several different notations: decimal, octal, hexadecimal. Octal numbers can be easily confused with decimal numbers and can lead to some… interesting consequences!

PHP supports two different notations for expressing floats: decimal and exponential. For example 1e2 equals 100. Floats can be as wide as your processor supports. It will be longer on 64 bits systems compared to 32 bits. Be aware that PHP does not track overflows so any operation with big scary numbers can have catastrophic consequences on the reliability of your application. Also be aware that basic operations with floats are not always precise. For example: echo (int) ((0.1 + 0.7) * 10); would output 7 instead of 8. Because internally in PHP the float value is 7.99999 and when casted to an integer becomes 7. To avoid this use extensions such as BCMath.

Strings are ordered collections of binary data. They can store anything from text to music recordings.

Boolean when converted from an integer becomes false if the integer is zero and becomes true otherwise. A string is converted to false only if it is empty or if it contains the single character. If it contains any other data—even multiple zeros—it is converted to true. When converted to a number or a string, a Boolean becomes 1 if it is true, and
0 otherwise.

Arrays are containers of ordered data elements; an array can be used to store nd retrieve any other data type, including numbers, Boolean values, strings, bjects and even other arrays.

Objects are containers of both data and code. They form the basis of Object oriented programming also known as OOP.

NULL indicates that a variable has no value. A variable is considered to be NULL if it has been assigned the special value NULL, or if it has not yet been assigned value at all.

The resource data type is used to indicate external resources that are not used atively by PHP, but that have meaning in the context of a special operation— such as, for example, handling files or manipulating images.

You can force PHP to convert some types to others. For example: echo (int) $x; Though you cannot convert any data types to resources though vice versa is available to get hold of a resource ID.

1.4 Variables

Variables can only be named letters, numbers, underscores. A variable can only start with an alpha character or an underscore. Variables and constants are the only two identifier types that are case sensitive.

PHP supports variables variables:

$name = '123';
/* 123 is your variable name, this would normally be invalid. */
$$name = '456';
echo ${'123'};

Variables can also hold function names and functions can be called through variables like this:
$f = ’myFunc’;
$f(); // will call myFunc();

To determine whether a variable exists use isset(). It will return true when a variable is defined and is not NULL.

1.5 Constants

Constants can only contain scalar values and follow the same naming conventions as variables. They are also case sensitive.

RSS Feed

2 Comments for Zend PHP certificate study guide overview (part I)

nathaniel | May 1, 2008 at 12:55 AM

Your statement, “..A variable can only start with a number or an underscore..” I needs the “number” switched to “alpha-character”

The Developer Day » Blog Archive » PHP Zend Certification Exam | September 9, 2008 at 11:16 PM

[...] Previously i wrote that i would like to take the Zend Certification Exam and i thought i could review every chapter of the Zend Certification Exam study guide. Well i think i have lost my inspiration somwhere along the way. Though i recently found a blog that already did what i wanted to do. Check it out and see if it helps. I also found out about Paul Reinheimer’s Zend Certification Exam course which is available as a free PDF on the internet and could be really useful to prepare. [...]

Leave a comment!

<<

>>

Find it!

Theme Design by devolux.org