Data Formats & Types – PHP Certification Exam Series [9]

PHP Data Format - PHP Certification Exam Notes

Introduction: This is the ninth part of my study notes for the Zend PHP Certification Exam. You can read more about my PHP Certification exam journey here.

XML (Extensible Markup Language)

  • important for data exchange, e.g. RSS
  • a sub-set of SGML (Standard Generalized Markup Language)
  • Entity: a named unit of storage, e.g. to represent ampersand character
  • Element: a data object that is part of the XML document, can have core or more attributes (as peripheral information)
  • DTD (Document Type Declaration) or XSD (XML Schema Definition): set of instructions for the valid XML definition

Well Formed

  • Single root level tag
  • Tags must be opened and closed properly
  • Entities must be well formed


  • Well formed
  • Validated against DTD/XSD

Parsing XML

XML Extension

  • Create XML parsers (+ params) and define handlers:
  • xml_parser_create($encoding); … xml_parser_free($xmlparser); // free the parser
  • xml_parser_create_ns($encoding,$separator=’:’)create an XML parser with namespace support
  • xml_set_element_handler(parser,start,end)[specify functions to be called at the start and end of an element in the XML document]
  • xml_set_object($parser, $object) allow a parser to be used within an object
  • xml_parse_into_struct($parser,$xml,$valueArr,$indexArr) parse XML data into an array structure
  • requires libxml extension and expat library (enabled by default)
  • source encoding: at parsing, cannot be changed during parser lifetime, UTF-8 (for PHP internal document representation), ISO-8859-1 (default), characters not included in the source encoding will cause errors
  • target encoding: at PHP passing data to XML handlers, encoding can be changed at any time, characters not included in the target encoding are demoted to ?
  • the XSL Extension is used to apply XML transformations


  • Document Object Model
  • uses UTF-8 encoding
  • requires libxml2 extension (Gnome XML Library) and expat library (enabled by default)
  • Loads entire document into ram, then creates an internal tree representation


  • Simple access to XML data from PHP
  • requires the simpleXML extension, supports only version 1.0 of XML specifications
  • elements become object properties, attributes can be accessed via associative arrays
  • all objects are instances of the SimpleXMLElement class


  • $xml = simplexml_load_string($library);- load xml from string
  • $xml = simplexml_load_file ( string $filename [, string $class_name [, int $options [, string $ns [, bool $is_prefix]]]] )- load xml from file (no need to file_get_contents() first)
  • $xml = new SimpleXMLElement();
  • SimpleXMLElement::construct($string/filename,$options,$data_is_url,$ns,$is_ns_prefix) – create a simpleXMLElement object with the supplied information, an E_WARNING is thrown if errors in XML, set $data_is_url to true if a $filename is supplied as the 1st argument
  • SimpleXMLElement::children() – find the children of the node as an iterator
  • SimpleXMLElement::addChild() – add a child (PHP>5.1.3)
  • SimpleXMLElement::count() – count the number of children of an element
  • SimpleXMLElement::getName() – retrieve the element’s name
  • SimpleXMLElement::attributes() – identifies the element’s attributes
  • SimpleXMLElement::addAttributes() – add an attribute
  • SimpleXMLElement::xpath() – run an path query on xml and return an array
  • SimpleXMLElement::asXML($filepath) – to return the object as a well-formed XML, if $filepath is set, the XML is output to the file
  • $library->book[0] = NULL; – to remove child elements, however the attribute of book will NOT be removed, need to use DOM to remove completely


  • <root xmlns=”” xmlns:meta=…  xmlns:pub=…   xmlns:foo=… >
  • SimpleXMLElement::getDocNamespaces() – an array of namespaces declared in root, passing TRUE as the argument will return all namespaces declared also in the children tags
  • SimpleXMLElement::getNamespaces() – an array of namespaces actually used, passing TRUE as the argument will return all namespaces declared also in the children tags

DOM Document

$dom = new DomDocument();
$dom->load("library.xml"); or $dom->loadXML($xml); // $xml is a string$dom->loadHTMLFile("library.html"); or $dom->loadHTML($html);
$dom->save(); // (to a file)
$dom->saveXML(); // (to a string)
$dom->saveHTML(); // (also to a string, but saves an HTML document instead of an XML file)
$dom->saveHTMLFile(); // (to a file in HTML format).
$xpath = new DomXpath($dom); // use XPath
$result = $xpath->query("//lib:title/text()"); // to execute XPath query
foreach($result as $book) {echo $book->data;} // $result->length gives the no. of items


  • DomDocument::createElement() – create the element ONLY, need appendChild() to actually add to the xml, e.g. $book= $dom->createElement(“ns1:book”); $node->setAttribute(’xmlns:ns1’, ’’); $book->setAttribute(“meta:isbn”,”1234567″); OR
  • DomDocument::createElementNS() –  $dom->createElement(“″,”ns1:book”);
  • DomDocument::documentElement() –  return the root element
  • DomDocument::createTextNode() – create a text node only, e.g. $text = $dom->createTextNode(“123”);
  • DomNode::appendChild() – if operate on an existing node, it will move the node
  • DomNode::insertBefore() – if operate on an existing node, it will move the node
  • DomNode::parentNode() – move to parent node
  • DomNode::cloneNode() – clone the current node
  • DomNode::setAttributeNS()
  • DomDocument::saveXML() – creates an XML document from the DOM representation
  • DomDocument::save() – saves XML document from the DOM representation in a file


  • DomNode::removeAttribute() – e.g. $result->item(1)->removeAttribute(’type’);
  • DomNode::removeChild()- e.g. $result->item(0)->parentNode->removeChild($result->item(0));
  • DomCharacterData::deleteData() – e.g. $result->item(0)->deleteData(0, $result->item(0)->length); // offset = 0 and length = item(0)->length i.e. all the text

DOM and SimpleXML

  • dom_import_simplexml($sxml) – convert a simpleXML object into a DOM
  • simplexml_import_dom($dom); – convert a DOM node into a simpleXML object

Pre-defined Constants

  • XML_ELEMENT_NODE – as a DOMElement
  • XML_TEXT_NODE – as a DOMText


  • query language used to select nodes within an XML document
  • $xpath = new DomXpath($dom);
  • A call to DomXpath::query() will return a DomNodeList object;

XPath Searches

  • xpath(“item”) – will return an array of item objects
  • xpath(“/bookshelf”) – will return all children of the forum node
  • xpath(“//book”) – will return an array of title values
  • xpath(“.”) – will return the current node, <bookshelf>
  • xpath(“..”) – will return an empty array, root node <bookshelf> does not have a parent element

Web Services

  • provide a standard means of interoperating between different software applications, running on a variety of platforms and/or frameworks
  • use XML for communication
  • three popular types: XML-RPC, SOAP, REST (PHP5 contains tools for SOAP and REST) [bold – native support]
  • easier and more reliable than scraping pages


  • no native support from PHP5
  • Representational State Transfer
  • the focus is on the presence of resources in the system
  • in a loose sense,Web sites that provide RSS and RDF feeds provide a RESTful service.
  • REST allows you to interact with minimal overhead with clients as diverse as mobile phones and other websites
  • Design Standard: user only HTTP, stateless, exposes URIs, transfer XML/JSON or both
  • Requests look like filled out forms, can use either GET or POST
  • Verbs: GET, POST(create), PUT(update), DELETE
  • Response is an XML/JSON document
  • Data types supported: ASCII strings, integers, booleans
  • Request and Response is defined by the service provider
  • Services that use the REST architecture are referred to as RESTful services; those who use or provide RESTful services are sometimes humorously referred to as RESTafarians.
  • Request headers: CONTENT-TYPE: what is being provided, ACCEPT: what is expected
  • Status codes: 201 [created], 400 [bad request], 401 [unauthorized], 204 [no content], 500 [application error]
  • Response headers: CONTENT-TYPE: what is being returned, ACCEPT: what and when to cache
  • Response contents depend on the request headers/URI to provide the output accordingly (context switching), e.g. thru browser detection, request from XMLHttpRequest instead of browser
  • use CURL to send more complex header requests


  • allows the definition and exchange of complex data types in both the request and response, as well as providing a mechanism for various messaging patterns, the most common of which is the Remote Procedure Call (RPC)
  • originally acronym for Simple Object Access Protocol, extension used to write SOAP servers and clients
  • sent in a SOAP envelope that is an XML wrapper for data read and generated by the SOAP server a SOAP message contains: envelope element, header element, body element and fault element
  • a SOAP Web service is defined by using a WSDL (Web Service Description Language)
  • describes the methods offered by the web service, the parameters required, and the return type, as well as all complex types required by the service SOAP cache functions are affected by php.ini settings (soap.wsdl_cache_*)
  • requests are sent using POST
  • require libxml extension
  • responses are XML documents with similar Envelope & Body sections
  • Functions: is_soap_fault($result) (check whether SOAP call failed), use_soap_error_handler($handler=TRUE) (whether to use the SOAP error handler)
  • usually wrap inside a try…catch block to cache an exceptions


The functions (generally) take a WSDL file as input, and create an object that mimics the services of the web service:

$client = new SoapClient("");
API call
$result = $client->KeywordSearchRequest($params);
$client = new SoapClient('', array('trace' => 1));


start with a class that contains the methods you wish to make available to the public through aWeb service and use it as the basis for a SoapServer instance
NOT generate WSDL files automatically (unlike NuSOAP, Zend Studio)
may operate on the non-WSDL mode

$options = array('uri' => '');
$server = new SoapServer(NULL, $options);


  • The time() function is used to retrieve the current time measured in the number of seconds since the Unix Epoch (January 1, 1970, 00:00:00 GMT)
  • time is stored as a 64-bit number date.
  • * functions are affected by php.ini settings (e.g. date.default_latitude; date.timezone; )
  • the DateTime class with many static methods
  • DateTime::_construct($datetime,$timezone); // $datetime = time(); $timezone = NULL
  • DateTime::getTimestamp();  // get the current timestamp
  • DateTime::format($format);  // return the formatted time
  • DateTime::diff($datetime2);  // return the difference between $datetime2 and $datetime
  • mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] ) – return a unix timestamp for a date // it automatically calculates the correct value for an out-of-range input.
  • localtime() // get the local time as an array
  • idate()  // formate a local time as integer, e.g. idate(“d”); return today’s date
  • gmmktime () // passed parameters of this function represents a GMT date (Greenwich Mean Time)
  • date_add(): It is used to add a number of days, months, years, hours, minutes, and seconds to a DateTime object.
  • date_create_from_format(): It returns a new DateTime object formatted according to the given format.
  • date_diff(): It returns the difference between two DateTime objects.
  • date_get_last_errors(): It returns the warnings and errors from the last date/time operation.
  • date_parse_from_format(): It is used to retrieve information about a given date.
  • date_sub(): It is used to subtract a number of days, months, years, hours, minutes, and seconds from a DateTime object.
  • timezone_version_get(): It returns the version of the timezonedb.


date.timezone = “Asia/Hong_Kong”; // in php.ini OR

$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
$now = time(); echo 'Today is '.date('d/m/Y'); // Today is 04/06/2013

$objDateTime = new DateTime('2000-01-01', new DateTimeZone('Asia/Hong_Kong'));
$objDateTime->modify('+1 day');
echo $objDateTime->format('c');

Date formats

  • d – The day of the month (from 01 to 31)
  • D – A textual representation of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full textual representation of a day
  • N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z – The day of the year (from 0 through 365)
  • W – The ISO-8601 week number of year (weeks starting on Monday)
  • F – A full textual representation of a month (January through December)
  • m – A numeric representation of a month (from 01 to 12)
  • M – A short textual representation of a month (three letters)
  • n – A numeric representation of a month, without leading zeros (1 to 12)
  • t – The number of days in the given month
  • L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
  • o – The ISO-8601 year number
  • Y – A four digit representation of a year
  • y – A two digit representation of a year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • h – 12-hour format of an hour (01 to 12)
  • H – 24-hour format of an hour (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)
  • u – Microseconds (added in PHP 5.2.2)
  • e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
  • O – Difference to Greenwich time (GMT) in hours (Example: +0100)
  • P – Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T – Timezone abbreviations (Examples: EST, MDT)
  • Z – Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
  • c – The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
  • r – The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
  • U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

and the following predefined constants can also be used (available since PHP 5.1.0):

  • DATE_ATOM – Atom (example: 2013-04-12T15:52:01+00:00)
  • DATE_COOKIE – HTTP Cookies (example: Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_ISO8601 – ISO-8601 (example: 2013-04-12T15:52:01+0000)
  • DATE_RFC822 – RFC 822 (example: Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC850 – RFC 850 (example: Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_RFC1036 – RFC 1036 (example: Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC1123 – RFC 1123 (example: Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC2822 – RFC 2822 (Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC3339 – Same as DATE_ATOM (since PHP 5.1.3)
  • DATE_RSS – RSS (Fri, 12 Aug 2013 15:52:01 +0000)
  • DATE_W3C – World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00)


  • JSON (Javascript Object Notation)
  • is an extension loaded by default
  • object: {}, array: []
   "employees": [
      { "firstName":"John" , "lastName":"Doe" },
      { "firstName":"Peter" , "lastName":"Jones" }
  • Predefined constants: JSON_ERROR_NONE, JSON_ERROR_SYNTAX, JSON_ERROR_UTF8 (encoding issues), JSON_FORCE_OBJECT (if an empty PHP array is passed, the receiving end will get an object)
  • json_decode($json, $assoc=false, $depth) – decode a JSON, $assoc==true will convert into an associative array, otherwise will convert to a stdclass object
  • json_encode($value, $options) – return JSON
  • json_last_error() – return the last error in case of failure



You can read more about my PHP Certification exam journey here.

Support website running for FREE, thanks!

If you find this post helpful and if you are thinking of buying from Amazon, please support the running cost of this website at no extra cost to you by searching and buying through the search box below. Thank you very much for your help!

Edward Chung

Edward Chung aspires to become a full-stack web developer and project manager. In the quest to become a more competent professional, Edward studied for and passed the PMP Certification, ITIL v3 Foundation Certification, PMI-ACP Certification and Zend PHP Certification. Edward shares his certification experience and resources here in the hope of helping others who are pursuing these certification exams to achieve exam success.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

4 Responses

  1. Rahul says:

    Dear Edward Chung,
    Is there any criteria to pass ZCE exam that some one need to pass every section individually (i.e. Every topic need to pass individually) ?

    Thank you in advance for your knowledge sharing.

  2. Brooke says:

    This is super helpful! Thanks for sharing!

    Small correction: $data_is_url not $date_is_url