25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14 ” <strong>PHP</strong> Basics<br />

}<br />

echo ’myFunc!’;<br />

$f = ’myFunc’;<br />

$f(); // will call myFunc();<br />

Clearly, this technique should be used with as much care as variable variables, as the<br />

opportunities for mistakes and security issues it raises are quite significant.<br />

Determining If a Variable Exists<br />

One of the downsides of the way <strong>PHP</strong> handles variables is that there is no way to<br />

ensure that any one of them will exist at any given point in the execution of a script.<br />

This can introduce a range of problems—from annoying warnings if you try output<br />

the value of a non-existent variable to significant security and functionality issues<br />

when variables are unexpectedly unavailable when you need them.<br />

To mitigate this problem, you can use the special construct isset():<br />

echo isset ($x);<br />

A call to isset() will return true if a variable exists and has a value other than NULL.<br />

Constants<br />

Conversely to variables, constants are meant for defining immutable values. Constants<br />

can be accessed for any scope within a script; however, they can only contain<br />

scalar values. Constant names, like variables, are case-sensitive; they also follow the<br />

same naming requirements, with the exception of the leading $. It is considered best<br />

practice to define constants using only upper-case names.<br />

Here’s an example of constants at work:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

define(’EMAIL’, ’davey@php.net’); // Valid name<br />

echo EMAIL; // Displays ’davey@php.net’<br />

define(’USE_XML’, true);<br />

if (USE_XML) { } // Evaluates to true

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!