<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2687042700778634400</id><updated>2024-08-29T04:43:40.018+05:30</updated><category term="PHP"/><category term="php basics"/><category term="variables"/><category term="identifiers"/><category term="web pages"/><category term="array"/><category term="data types"/><category term="function"/><category term="string"/><category term="object"/><category term="object oriented php"/><category term="OOP"/><category term="Configuration Directives"/><category term="Windows"/><category term="function parameters"/><category term="Linux"/><category term="configure php"/><category term="control structure"/><category term="download and install"/><category term="php.ini"/><category term="Apache"/><category term="Testing"/><category term="constants"/><category term="expressions"/><category term="operands"/><category term="operators"/><category term="output statements"/><category term="Increment and Decrement Operators"/><category term="adding array elements"/><category term="associativity"/><category term="delimiters"/><category term="get"/><category term="post"/><category term="scope"/><category term="scripting"/><category term="super global"/><category term="syntax hghlighting"/><category term=".htaccess"/><category term="Converting Between Data Types"/><category term="Creating Arrays"/><category term="Data Handling"/><category term="Dynamic Extension"/><category term="Encapsulation"/><category term="Helper functions"/><category term="IIS"/><category term="Key concepts of OOP in PHP"/><category term="Language options"/><category term="Logical Operators"/><category term="Resource limits"/><category term="String Interpolation"/><category term="Testing arrays"/><category term="Type Casting"/><category term="arithmetic operators"/><category term="array differences"/><category term="array intersections"/><category term="array size and uniqueness"/><category term="array()"/><category term="array_chunk()"/><category term="array_combine()"/><category term="array_count_values"/><category term="array_diff()"/><category term="array_diff_assoc()"/><category term="array_flip"/><category term="array_intersect()"/><category term="array_intersect_assoc()"/><category term="array_key_exists"/><category term="array_keys"/><category term="array_merge()"/><category term="array_merge_recursive()"/><category term="array_rand()"/><category term="array_reverse"/><category term="array_search"/><category term="array_slice()"/><category term="array_splice()"/><category term="array_sum()"/><category term="array_unique"/><category term="array_values"/><category term="array_walk()"/><category term="arsort"/><category term="asort"/><category term="assignment operators"/><category term="autoloading objects"/><category term="bitwise operators"/><category term="boolean"/><category term="break statement"/><category term="class methods"/><category term="class properities"/><category term="class properties"/><category term="classes"/><category term="code editor"/><category term="comments"/><category term="comparison operators"/><category term="compound"/><category term="conditional statements"/><category term="continue statement"/><category term="contructors and destructors"/><category term="conversion specifier"/><category term="converting types"/><category term="cookies"/><category term="count()"/><category term="creating a function"/><category term="curly braces"/><category term="current()"/><category term="declaring methods"/><category term="declaring properties"/><category term="default argument values"/><category term="do..while statement"/><category term="double quotes"/><category term="each()"/><category term="echo"/><category term="else"/><category term="elseif"/><category term="end()"/><category term="environment"/><category term="equality operators"/><category term="escape sequence"/><category term="file inclusion statements"/><category term="float"/><category term="fopen wrappers"/><category term="for statement"/><category term="foreach statement"/><category term="function libraries"/><category term="function within function"/><category term="global"/><category term="goto statement"/><category term="heredoc"/><category term="httpd.conf"/><category term="if"/><category term="in_array"/><category term="include()"/><category term="inheritance"/><category term="instanceof keyword"/><category term="integer"/><category term="introduction"/><category term="invoking a function"/><category term="invoking methods"/><category term="invoking properties"/><category term="is_array()"/><category term="key()"/><category term="krsort()"/><category term="ksort()"/><category term="list()"/><category term="local"/><category term="locating array elements"/><category term="looping structures"/><category term="merging and slicing arrays"/><category term="method scopes"/><category term="miscellaneous"/><category term="natcasesort()"/><category term="natsort()"/><category term="next()"/><category term="nowdoc"/><category term="outputting arrays"/><category term="parent constructors"/><category term="passing arguments by reference"/><category term="passing arguments by value"/><category term="path directories"/><category term="polymorphism"/><category term="pop"/><category term="possibility"/><category term="power"/><category term="practicality"/><category term="precedence"/><category term="prev()"/><category term="price"/><category term="print"/><category term="print_r"/><category term="printf"/><category term="property overloading"/><category term="property scopes"/><category term="push"/><category term="random set of keys"/><category term="range()"/><category term="recursive functions"/><category term="removing array elements"/><category term="require()"/><category term="reset()"/><category term="retrieving types"/><category term="return statement"/><category term="revision"/><category term="rsort"/><category term="safe mode"/><category term="scalar"/><category term="server and client info"/><category term="sessions"/><category term="setting and getting properties"/><category term="shift"/><category term="shuffle()"/><category term="shuffling array elements"/><category term="single quotes"/><category term="sort"/><category term="sorting arrays"/><category term="splicnig an array"/><category term="sprintf"/><category term="static"/><category term="static class members"/><category term="string operators"/><category term="subdividing array elements"/><category term="switch"/><category term="traversing arrays"/><category term="type casting operators"/><category term="type hinting"/><category term="type juggling"/><category term="unrelated constructors"/><category term="unshift"/><category term="using type hinting"/><category term="usort()"/><category term="vprintf"/><category term="vsprintf"/><category term="web hosting provider"/><category term="while statement"/><title type='text'>Let us do | php</title><subtitle type='html'>Blog on php learning from basics to pro, lecturewise</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>57</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-1736859269825316508</id><published>2013-07-29T23:37:00.000+05:30</published><updated>2013-08-01T23:56:52.753+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="autoloading objects"/><category scheme="http://www.blogger.com/atom/ns#" term="Helper functions"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Helper Functions in Object Oriented PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
A number of functions are available to help the developer manage and use class libraries. These functions (&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot; title=&quot;functions&quot;&gt;&lt;strong&gt;more about functions in PHP&lt;/strong&gt;&lt;/a&gt;)&amp;nbsp; are introduced in this section.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;h1&gt;
Creating a Class Alias&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The class_alias() function creates a class alias, allowing the class (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about classes in PHP&lt;/a&gt;&lt;/strong&gt;) to be referred to by more than one name. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;boolean class_alias(string originalClassName, string aliasName)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
This function was introduced in PHP 5.3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Determining Whether a Class Exists&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The class_exists() function returns TRUE if the class specified by class_name exists within the currently executing script context and returns FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean class_exists(string class_name)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Determining Object Context&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The get_class() function returns the name of the class to which object (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about objects in PHP&lt;/a&gt;&lt;/strong&gt;) belongs and returns FALSE if object is not an object. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;string get_class(object object)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Learning about Class Methods&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The get_class_methods() function returns an array containing all method names defined by the class class_name. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array get_class_methods(mixed class_name)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Learning about Class Properties&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The get_class_vars() function returns an associative (&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot; rel=&quot;tag&quot; title=&quot;arrays&quot;&gt;&lt;strong&gt;array more about arrays in PHP&lt;/strong&gt;&lt;/a&gt;) containing the names of all properties and their corresponding values defined within the class specified by class_name. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array get_class_vars(string class_name)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Learning about Declared Classes&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function get_declared_classes() returns an array containing the names of all classes defined within the currently executing script. The output of this function will vary according to how your PHP distribution is configured (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/configuring-php.html&quot;&gt;more about configuring PHP&lt;/a&gt;)&lt;/strong&gt;. For instance, executing get_declared_classes() on a test server produces a list of 97 classes. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array get_declared_classes(void)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Learning about Object Properties&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function get_object_vars() returns an associative array containing the defined properties available to object and their corresponding values. Those properties that don’t possess a value will be assigned NULL within the associative array. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array get_object_vars(object object)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Determining an Object’s Parent Class&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The get_parent_class() function returns the name of the parent of the class to which object belongs. If object’s class is a base class, that class name will be returned. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;string get_parent_class(mixed object)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Determining Interface Existence&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The interface_exists() function determines whether an interface exists, returning TRUE if it does and FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean interface_exists(string interface_name [, boolean autoload])&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Determining Object Type&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The is_a() function returns TRUE if object belongs to a class of type class_name or if it belongs to a class that is a child of class_name. If object bears no relation to the class_name type, FALSE is returned. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;boolean is_a(object object, string class_name)&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Oddly, this function was temporarily deprecated from PHP 5.0.0 to PHP 5.3.0, causing an E_STRICT warning to be displayed during this time.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Determining Object Subclass Type&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The is_subclass_of() function returns TRUE if object (which can be passed in as type string or object) belongs to a class inherited from class_name and returns FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean is_subclass_of(mixed object, string class_name)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
Determining Method Existence&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The method_exists() function returns TRUE if a method named method_name is available to object and returns FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean method_exists(object object, string method_name)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Autoloading Objects&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
For organizational reasons, it’s common practice to place each class in a separate file. Returning to the library scenario, suppose the management application calls for classes representing books, employees, events, and patrons. Tasked with this project, you might create a directory named classes and place the following files in it: Books.class.php,&amp;nbsp; mployees.class.php, Events.class.php, and Patrons.class.php. While this does indeed facilitate class management, it also requires that each separate file be made available to any script requiring it, typically through the &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-8-control-structures-4.html&quot;&gt;require_once()&lt;/a&gt;&lt;/strong&gt; statement. Therefore, a script requiring all four classes would require that the following statements be inserted at the beginning:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
require_once(&quot;classes/Books.class.php&quot;);&lt;br /&gt;
require_once(&quot;classes/Employees.class.php&quot;);&lt;br /&gt;
require_once(&quot;classes/Events.class.php&quot;);&lt;br /&gt;
require_once(&quot;classes/Patrons.class.php&quot;);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Managing class inclusion in this manner can become rather tedious and adds an extra step to the already often complicated development process. To eliminate this additional task, the concept of auto loading objects was introduced in PHP 5. Autoloading allows you to define a special __autoload function that is automatically called whenever a class is referenced that hasn’t yet been defined in the script. You can eliminate the need to manually include each class file by defining the following function:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&lt;br /&gt;
function __autoload($class) {&lt;br /&gt;
require_once(&quot;classes/$class.class.php&quot;);&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Defining this function eliminates the need for the require_once() statements because when a class is invoked for the first time, __autoload() will be called, loading the class according to the commands defined in __autoload(). This function can be placed in a &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;global application configuration file&lt;/span&gt;&lt;/em&gt;, meaning only that function will need to be made available to the script.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/more-components-of-object-oriented-php.html&quot;&gt;More components of&amp;nbsp;Object Oriented PHP&lt;/a&gt;&amp;nbsp;&amp;lt;-&amp;gt; Advanced Object Oriented PHP&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/1736859269825316508/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/helper-functions-in-object-oriented-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1736859269825316508'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1736859269825316508'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/helper-functions-in-object-oriented-php.html' title='Helper Functions in Object Oriented PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-8505523446932498835</id><published>2013-07-28T23:16:00.000+05:30</published><updated>2013-08-01T23:51:21.014+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="instanceof keyword"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="static class members"/><title type='text'>More components of Object Oriented PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1&gt;
Static Class Members&lt;/h1&gt;
In the articles on properties and methods in classes in PHP namely: &lt;br /&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/class-properties-1.html&quot;&gt;Defining Class Properties in PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html&quot;&gt;Defining Class Methods in PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You were made acquainted with the declarations and scope of properties and methods in classes in PHP (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about classes in PHP&lt;/a&gt;&lt;/strong&gt;). But the Static scope of properties and methods was not introduced there to prevent confusion for the newbies. Confusion? Not really! It’s just for the sake of convenience. Now that you are comfortable with class’s components I can introduce &lt;em&gt;Static Scope&lt;/em&gt; here.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;strong&gt;&lt;span style=&quot;color: #333333;&quot;&gt;One of the major benefits to using static properties is that they keep their stored values for the duration of the script.&lt;/span&gt;&lt;/strong&gt;&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Sometimes it’s useful to create properties and methods that are not invoked by any particular object but rather are pertinent to and are shared by all class instances.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
It is similar to static variables discussed in article: &lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot; title=&quot;variables&quot;&gt;&lt;strong&gt;Variables in PHP&lt;/strong&gt;&lt;/a&gt;.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
For example, suppose that you are writing a class that tracks the number of web page visitors. You wouldn’t want the visitor count to reset to zero every time the class is instantiated, so you would set the property to be of the static scope:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Visitor&lt;br /&gt;
{&lt;br /&gt;
private static $visitors = 0;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
self::$visitors++;&lt;br /&gt;
}&lt;br /&gt;
static function getVisitors()&lt;br /&gt;
{&lt;br /&gt;
return self::$visitors;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
// Instantiate the Visitor class.&lt;br /&gt;
$visits = new Visitor();&lt;br /&gt;
echo Visitor::getVisitors().&quot;&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
// Instantiate another Visitor class.&lt;br /&gt;
$visits2 = new Visitor();&lt;br /&gt;
echo Visitor::getVisitors().&quot;&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
The results are as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
1&lt;br /&gt;
2&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Because the $visitors property was declared as static, any changes made to its value (in this case via the &lt;strong&gt;class constructor&lt;/strong&gt;) are reflected across all &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;instantiated objects&lt;/a&gt;&lt;/strong&gt;.&lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt; Also note that static properties and methods are referred to using the self keyword and class name, rather than via $this and arrow operators. This is because referring to static properties using the means allowed for their “regular” siblings is not possible and will result in a syntax error if attempted.&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;strong&gt;Note:&lt;/strong&gt; You can’t use $this within a class to refer to a property declared as static.&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1&gt;
The instanceof Keyword&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The instanceof keyword was introduced with PHP 5. With it you can determine whether an object is an instance of a class, is a subclass of a class, or implements a particular interface, and do something accordingly. For example, suppose you want to learn whether $manager is derived from the class Employee:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$manager = new Employee();&lt;br /&gt;
...&lt;br /&gt;
if ($manager instanceof Employee) echo &quot;Yes&quot;;&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The class name is not surrounded by any sort of delimiters (quotes). Including them will result in a syntax error.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The instanceof keyword is particularly useful when you’re working with a number of objects simultaneously. For example, you might be repeatedly calling a particular function but want to tweak that function’s behavior in accordance with a given type of object. You might use a case statement and the instanceof keyword to manage behavior in this fashion.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/using-constructors-and-destructors.html&quot;&gt;Using Constructors and Destructors in PHP&lt;/a&gt;&amp;nbsp;&amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/helper-functions-in-object-oriented-php.html&quot;&gt;Helper Functions in OOP PHP&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/8505523446932498835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/more-components-of-object-oriented-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8505523446932498835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8505523446932498835'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/more-components-of-object-oriented-php.html' title='More components of Object Oriented PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-8261739569807231520</id><published>2013-07-27T23:34:00.000+05:30</published><updated>2013-08-01T23:25:37.947+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="contructors and destructors"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="parent constructors"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="unrelated constructors"/><title type='text'>Using Constructors and Destructors in PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Often, you’ll want to execute a number of tasks when creating and destroying objects. For example, you might want to immediately assign several properties of a newly instantiated object. However, if you have to do so manually, you’ll almost certainly forget to execute all of the required tasks. Object-oriented programming goes a long way toward removing the possibility for such errors by offering special methods, called &lt;strong&gt;&lt;em&gt;constructors and destructors&lt;/em&gt;&lt;/strong&gt;, that automate the object creation and destruction processes.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Constructors&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
You often want to initialize certain properties and even trigger the execution of methods (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html&quot;&gt;more about methods in PHP&lt;/a&gt;&lt;/strong&gt;) found when an object (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about objects in PHP&lt;/a&gt;&lt;/strong&gt;) is newly instantiated. There’s nothing wrong with doing so immediately after instantiation, but it would be easier if this were done for you automatically. Such a mechanism exists in OOP, known as a &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;constructor&lt;/span&gt;&lt;/em&gt;. Quite simply, &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;a constructor is defined as a block of code that automatically executes at the time of object instantiation&lt;/span&gt;&lt;/em&gt;. OOP constructors offer a number of advantages:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Constructors can accept parameters, which are assigned to specific object properties at creation time.  &lt;/li&gt;
&lt;li&gt;Constructors can call class methods or other functions.  &lt;/li&gt;
&lt;li&gt;Class constructors can call on other constructors, including those from the class parent.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
This section reviews how all of these advantages work with PHP 5’s improved constructor functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; PHP 4 also offered class constructors, but it used a different, more cumbersome syntax than the one used in version 5. Version 4 constructors were simply class methods of the same name as the class they represented. Such a convention made it tedious to rename a class. The new constructor-naming convention resolves these issues. For reasons of compatibility, however, if a class is found to not contain a constructor satisfying the new naming convention, that class will then be searched for a method bearing the same name as the class; if located, this method is considered the constructor.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
PHP recognizes constructors by the name &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;__construct&lt;/span&gt;&lt;/em&gt; (&lt;em&gt;a double underscore precedes the constructor keyword&lt;/em&gt;). The general syntax for constructor declaration follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;function __construct([argument1, argument2, ..., argumentN])&lt;br /&gt;{&lt;br /&gt;// Class initialization code&lt;br /&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
As an example, suppose you want to immediately populate certain book properties with information specific to a supplied ISBN. For example, you might want to know the title and author of a book, in addition to how many copies the library owns and how many are presently available for loan. The code might look like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Book&lt;br /&gt;
{&lt;br /&gt;
private $title;&lt;br /&gt;
private $isbn;&lt;br /&gt;
private $copies;&lt;br /&gt;
function __construct($isbn)&lt;br /&gt;
{&lt;br /&gt;
$this-&amp;gt;setIsbn($isbn);&lt;br /&gt;
$this-&amp;gt;getTitle();&lt;br /&gt;
$this-&amp;gt;getNumberCopies();&lt;br /&gt;
}&lt;br /&gt;
public function setIsbn($isbn)&lt;br /&gt;
{&lt;br /&gt;
$this-&amp;gt;isbn = $isbn;&lt;br /&gt;
}&lt;br /&gt;
public function getTitle() {&lt;br /&gt;
$this-&amp;gt;title = &quot;Easy PHP Websites with the Zend Framework&quot;;&lt;br /&gt;
print &quot;Title: {$this-&amp;gt;title}&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
public function getNumberCopies() {&lt;br /&gt;
$this-&amp;gt;copies = &quot;5&quot;;&lt;br /&gt;
print &quot;Number copies available: {$this-&amp;gt;copies}&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$book = new book(&quot;0615303889&quot;);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
This results in the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Title: Easy PHP Websites with the Zend Framework&lt;br /&gt;
Number copies available: 5&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Of course, a real-life implementation would likely involve somewhat more intelligent get methods (e.g., methods that query a database), but the point is made. Instantiating the book object results in the automatic invocation of the constructor, which in turn calls the setIsbn(), getTitle(), and getNumberCopies() methods. If you know that such methods should be called whenever a new object is instantiated, you’re far better off automating the calls via the constructor than attempting to manually call them yourself.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Additionally, if you would like to make sure that these methods are called only via the constructor, you should set their scope to private, ensuring that they cannot be directly called by the object or by a subclass.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Invoking Parent Constructors&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP does not automatically call the parent constructor; you must call it explicitly using the parent keyword. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
protected $name;&lt;br /&gt;
protected $title;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Employee constructor called!&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
class Manager extends Employee&lt;br /&gt;
{&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
parent::__construct();&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Manager constructor called!&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Manager();&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
This results in the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Employee constructor called!&lt;br /&gt;
Manager constructor called!&lt;/div&gt;
&lt;br /&gt;
Neglecting to include the call to parent::__construct() results in the invocation of only the Manager constructor, like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Manager constructor called!&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Invoking Unrelated Constructors&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
You can invoke class constructors that don’t have any relation to the instantiated object simply by prefacing __construct with the class name, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;classname::__construct()&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
As an example, assume that the Manager and Employee classes used in the previous example bear no hierarchical relationship; instead, they are simply two classes located within the same library. The Employee constructor could still be invoked within Manager’s constructor, like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
Employee::__construct();&lt;/div&gt;
&lt;br /&gt;
Calling the Employee constructor in this manner results in the same outcome shown in the example.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Destructors&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Just as you can use constructors to customize the object creation process, so can you use destructors to modify the object destruction process. Destructors are created like any other method but must be titled __destruct(). An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Book&lt;br /&gt;
{&lt;br /&gt;
private $title;&lt;br /&gt;
private $isbn;&lt;br /&gt;
private $copies;&lt;br /&gt;
function __construct($isbn)&lt;br /&gt;
{&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Book class instance created.&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
function __destruct()&lt;br /&gt;
{&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Book class instance destroyed.&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$book = new Book(&quot;0615303889&quot;);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
Here’s the result:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Book class instance created.&lt;br /&gt;
Book class instance destroyed.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
When the script is complete, PHP will destroy any objects that reside in memory. Therefore, if the instantiated class and any information created as a result of the instantiation reside in memory, you’re not required to explicitly declare a destructor. However, if less volatile data is created (say, stored in a database) as a result of the instantiation and should be destroyed at the time of object destruction, you’ll need to create a custom destructor.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html&quot;&gt;Defining Class Methods&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/more-components-of-object-oriented-php.html&quot;&gt;More components of Object Oriented PHP&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/8261739569807231520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/using-constructors-and-destructors.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8261739569807231520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8261739569807231520'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/using-constructors-and-destructors.html' title='Using Constructors and Destructors in PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-8941010083669929763</id><published>2013-07-26T23:32:00.000+05:30</published><updated>2013-08-01T23:10:47.809+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="class methods"/><category scheme="http://www.blogger.com/atom/ns#" term="declaring methods"/><category scheme="http://www.blogger.com/atom/ns#" term="invoking methods"/><category scheme="http://www.blogger.com/atom/ns#" term="method scopes"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="type hinting"/><title type='text'>Defining Class Methods in PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
A method is quite similar to a function, except that it is intended to define the behavior of a particular class (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about classes in PHP&lt;/a&gt;&lt;/strong&gt;). Like a function (&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot; title=&quot;functions&quot;&gt;&lt;strong&gt;more about functions in PHP&lt;/strong&gt;&lt;/a&gt;), a method can accept arguments as input and can return a value to the caller. Methods are also invoked like functions, except that the method is prefaced with the name of the object (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;more about objects in PHP&lt;/a&gt;&lt;/strong&gt;) invoking the method, like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;$object-&amp;gt;methodName();&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In this article you’ll learn all about &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;methods, including method declaration, method invocation, &lt;/em&gt;&lt;span style=&quot;color: #333333;&quot;&gt;and&lt;/span&gt;&lt;em&gt; scope&lt;/em&gt;&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Declaring Methods&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Methods are created in exactly the same fashion as functions, using identical syntax. The only difference between methods and normal functions is that the method declaration is typically prefaced with a &lt;em&gt;&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html#scope_descriptor&quot;&gt;scope descriptor&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;. The generalized syntax follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;scope function functionName()&lt;br /&gt;{&lt;br /&gt;// Function body goes here&lt;br /&gt;}&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
For example, a public method titled calculateSalary() might look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
public function calculateSalary()&lt;br /&gt;
{&lt;br /&gt;
return $this-&amp;gt;wage * $this-&amp;gt;hours;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
In this example, the method is directly invoking two class properties, &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;wage &lt;/em&gt;&lt;span style=&quot;color: #333333;&quot;&gt;and&lt;/span&gt;&lt;em&gt; hours&lt;/em&gt;&lt;/span&gt;, using the &lt;strong&gt;&lt;span style=&quot;color: black;&quot;&gt;$this&lt;/span&gt;&lt;/strong&gt; keyword. It calculates a salary by multiplying the two property values together and returns the result just like a function might. Note, however, that a method isn’t confined to working solely with class properties; it’s perfectly valid to pass in arguments in the same way you can with a function.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;strong&gt;Tip:&lt;/strong&gt; In the case of public methods, you can forgo explicitly declaring the scope and just declare the method like you would a function (without any scope).&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1&gt;
Invoking Methods&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Methods are invoked in almost exactly the same fashion as functions. Continuing with the previous example, the calculateSalary() method would be invoked like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$employee = new Employee(&quot;Janie&quot;);&lt;br /&gt;
$salary = $employee-&amp;gt;calculateSalary();&lt;/div&gt;
&lt;br /&gt;
&lt;h1 id=&quot;scope_descriptor&quot;&gt;
Method Scopes&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP supports six method scopes: &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;public, private, protected, abstract, final, &lt;/em&gt;&lt;span style=&quot;color: #333333;&quot;&gt;and&lt;/span&gt;&lt;em&gt; static&lt;/em&gt;&lt;/span&gt;. The first five scopes are introduced in this article. The sixth, static, is introduced in the article “&lt;strong&gt;More components of Object Oriented PHP&lt;/strong&gt;”.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Public&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Public methods can be accessed from anywhere at any time. You declare a public method by prefacing it with the keyword public or by forgoing any prefacing whatsoever. The following example demonstrates both declaration practices, in addition to demonstrating how public methods can be called from outside the class:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Visitors&lt;br /&gt;
{&lt;br /&gt;
public function greetVisitor()&lt;br /&gt;
{&lt;br /&gt;
echo &quot;Hello&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
function sayGoodbye()&lt;br /&gt;
{&lt;br /&gt;
echo &quot;Goodbye&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
Visitors::greetVisitor();&lt;br /&gt;
$visitor = new Visitors();&lt;br /&gt;
$visitor-&amp;gt;sayGoodbye();&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is the result:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Hello&lt;br /&gt;
Goodbye&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Private&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Methods marked as private are available for use only within the originating class and cannot be called by the &lt;strong&gt;instantiated object&lt;/strong&gt;, nor by any of the originating class’s child classes (&lt;strong&gt;more about child classes in OOP model of PHP&lt;/strong&gt;). Methods solely intended to be helpers for other methods located within the class should be marked as private. For example, consider a method called &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;validateCardNumber()&lt;/span&gt;&lt;/em&gt; that is used to determine the syntactical validity of a patron’s library card number. Although this method would certainly prove useful for satisfying a number of tasks, such as creating patrons and self-checkout, the function has no use when executed alone. Therefore, validateCardNumber() should be marked as private, like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
private function validateCardNumber($number)&lt;br /&gt;
{&lt;br /&gt;
if (! ereg(&#39;^([0-9]{4})-([0-9]{3})-([0-9]{2})&#39;) ) return FALSE;&lt;br /&gt;
else return TRUE;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Attempts to call this method directly from an instantiated object result in a fatal error.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Protected&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Class methods marked as protected are available only to the originating class and its child classes. Such methods might be used for helping the class or subclass perform internal computations. For example, before retrieving information about a particular staff member, you might want to verify the employee identification number (EIN) passed in as an argument to the class instantiator. You would then verify this EIN for syntactical correctness using the verifyEIN() method. Because this method is intended for use only by other methods within the class and could potentially be useful to classes derived from&lt;br /&gt;
Employee, it should be declared as protected:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
private $ein;&lt;br /&gt;
function __construct($ein)&lt;br /&gt;
{&lt;br /&gt;
if ($this-&amp;gt;verifyEIN($ein)) {&lt;br /&gt;
echo &quot;EIN verified. Finish&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
protected function verifyEIN($ein)&lt;br /&gt;
{&lt;br /&gt;
return TRUE;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Employee(&quot;123-45-6789&quot;);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Attempts to call verifyEIN() from outside of the class or from any child classes will result in a fatal error because of its protected scope status.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Abstract&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Abstract methods are special in that they are declared only within a parent class but are implemented in child classes. Only classes declared as abstract can contain abstract methods. You might declare an abstract method if you want to define an application programming interface (API) that can later be used as a model for implementation. A developer would know that his particular implementation of that method should work provided that it meets all requirements as defined by the abstract method. Abstract methods are declared like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;abstract function methodName();&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Suppose that you want to create an abstract Employee class, which would then serve as the base class for a variety of employee types (manager, clerk, cashier, etc.):&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
abstract class Employee&lt;br /&gt;
{&lt;br /&gt;
abstract function hire();&lt;br /&gt;
abstract function fire();&lt;br /&gt;
abstract function promote();&lt;br /&gt;
abstract demote();&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
This class could then be extended by the respective employee classes, such as Manager, Clerk, and Cashier.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Final&lt;/span&gt;&lt;/h1&gt;
Marking a method as final prevents it from being overridden by a subclass. A finalized method is declared like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
final function getName() {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Attempts to later override a finalized method result in a fatal error.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;
Type Hinting&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Type hinting is a feature introduced with the PHP 5 release. Type hinting ensures that the object being passed to the method is indeed a member of the expected class. For example, it makes sense that only objects of class Employee should be passed to the takeLunchbreak() method. Therefore, you can preface the method definition’s sole input parameter $employee with Employee, enforcing this rule. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
private function takeLunchbreak(Employee $employee)&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Keep in mind that type hinting only works for objects and arrays. You can’t offer hints for types such as integers, floats, or strings.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html&quot;&gt;Defining Class Properties&lt;/a&gt;&amp;nbsp;&amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/using-constructors-and-destructors.html&quot;&gt;Using Constructors and destructors&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/8941010083669929763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-methods.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8941010083669929763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8941010083669929763'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-methods.html' title='Defining Class Methods in PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-5940027339095215640</id><published>2013-07-25T23:30:00.000+05:30</published><updated>2013-08-01T01:24:55.560+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="class properties"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="property overloading"/><category scheme="http://www.blogger.com/atom/ns#" term="setting and getting properties"/><title type='text'>Defining Class Properties in PHP :: continued</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1&gt;
Property Overloading&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Property overloading continues to protect properties by forcing access and manipulation through &lt;strong&gt;public methods&lt;/strong&gt;, yet allowing the data to be accessed as if it were a &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html&quot;&gt;public property&lt;/a&gt;&lt;/strong&gt;. These methods, known as&lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt; accessors and mutators&lt;/span&gt;&lt;/em&gt;, or more informally as&lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt; getters and setters&lt;/span&gt;&lt;/em&gt;, are automatically triggered whenever the property is accessed or manipulated, respectively.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Unfortunately, PHP does not offer property overloading features that you might be used to if you’re familiar with other OOP languages such as C++ and Java. Therefore, you’ll need to make do with using public methods to imitate such functionality. For example, you might create getter and setter methods for the property name by declaring two functions, getName() and setName(), respectively, and embedding the appropriate syntax within each.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
PHP version 5 and newer does offer some semblance of support for property overloading, done by overloading the __set and __get methods. These methods are invoked if you attempt to reference a member variable that does not exist within the class definition. Properties can be used for a variety of purposes, such as to invoke an error message, or even to extend the class by actually creating new variables (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot; rel=&quot;tag&quot; target=&quot;_blank&quot; title=&quot;variables&quot;&gt;&lt;strong&gt;more about variables in PHP&lt;/strong&gt;&lt;/a&gt;) on the fly. Both __get and __set are introduced in this article.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1&gt;
Setting Properties with the __set() Method&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The mutator, or setter method, is responsible for both hiding property assignment implementation and validating class data before assigning it to a class property. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean __set([string property_name],[mixed value_to_assign])&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
It takes as input a property name and a corresponding value, returning TRUE if the method is successfully executed and FALSE otherwise. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
var $name;&lt;br /&gt;
function __set($propName, $propValue)&lt;br /&gt;
{&lt;br /&gt;
echo &quot;Nonexistent variable: \$$propName!&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Employee ();&lt;br /&gt;
$employee-&amp;gt;name = &quot;Mario&quot;;&lt;br /&gt;
$employee-&amp;gt;title = &quot;Executive Chef&quot;;&lt;/div&gt;
&lt;br /&gt;
This results in the following output:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Nonexistent variable: $title!&lt;/div&gt;
&lt;br /&gt;
You could use this method to actually extend the class with new properties, like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
public $name;&lt;br /&gt;
function __set($propName, $propValue)&lt;br /&gt;
{&lt;br /&gt;
$this-&amp;gt;$propName = $propValue;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Employee();&lt;br /&gt;
$employee-&amp;gt;name = &quot;Mario&quot;;&lt;br /&gt;
$employee-&amp;gt;title = &quot;Executive Chef&quot;;&lt;br /&gt;
echo &quot;Name: &quot;.$employee-&amp;gt;name;&lt;br /&gt;
echo &quot;&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
echo &quot;Title: &quot;.$employee-&amp;gt;title;&lt;/div&gt;
&lt;br /&gt;
This produces the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Name: Mario&lt;br /&gt;
Title: Executive Chef&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
&amp;nbsp;&lt;/h1&gt;
&lt;h1&gt;
Getting Properties with the __get() Method&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The accessor, or mutator method, is responsible for encapsulating the code required for retrieving a class variable. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;boolean __get([string property_name])&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
It takes as input one parameter, the name of the property whose value you’d like to retrieve. It should return the value TRUE on successful execution and FALSE otherwise. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
public $name;&lt;br /&gt;
public $city;&lt;br /&gt;
protected $wage;&lt;br /&gt;
function __get($propName)&lt;br /&gt;
{&lt;br /&gt;
echo &quot;__get called!&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
$vars = array(&quot;name&quot;,&quot;city&quot;);&lt;br /&gt;
if (in_array($propName, $vars))&lt;br /&gt;
{&lt;br /&gt;
return $this-&amp;gt;$propName;&lt;br /&gt;
} else {&lt;br /&gt;
return &quot;No such variable!&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Employee();&lt;br /&gt;
$employee-&amp;gt;name = &quot;Mario&quot;;&lt;br /&gt;
echo $employee-&amp;gt;name.&quot;&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
echo $employee-&amp;gt;age;&lt;/div&gt;
&lt;br /&gt;
This returns the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Mario&lt;br /&gt;
__get called!&lt;br /&gt;
No such variable!&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Creating Custom Getters and Setters&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Frankly, although there are some benefits to the __set() and __get() methods, they really aren’t sufficient for managing properties in a complex object-oriented application. Because PHP doesn’t offer support for the creation of properties in the fashion that Java or C# does, you need to implement your own solution. Consider creating two methods for each private property, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
private $name;&lt;br /&gt;
// Getter&lt;br /&gt;
public function getName() {&lt;br /&gt;
return $this-&amp;gt;name;&lt;br /&gt;
}&lt;br /&gt;
// Setter&lt;br /&gt;
public function setName($name) {&lt;br /&gt;
$this-&amp;gt;name = $name;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Although such a strategy doesn’t offer the same convenience as using properties, it does encapsulate management and retrieval tasks using a standardized naming convention. Of course, you should add additional validation functionality to the setter; however, this simple example should suffice to drive the point home.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html&quot;&gt;Defining Class Properties&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html&quot;&gt;Defining Class Methods&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/5940027339095215640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-properties-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5940027339095215640'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5940027339095215640'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-properties-2.html' title='Defining Class Properties in PHP :: continued'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-5419948957095425482</id><published>2013-07-24T00:58:00.000+05:30</published><updated>2013-08-01T01:24:55.556+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="class properities"/><category scheme="http://www.blogger.com/atom/ns#" term="declaring properties"/><category scheme="http://www.blogger.com/atom/ns#" term="invoking properties"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="property scopes"/><title type='text'>Defining Class Properties in PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Properties are attributes that are intended to describe some aspect of a class. They are quite similar to standard PHP variables (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot; rel=&quot;tag&quot; title=&quot;variables&quot;&gt;&lt;strong&gt;more about variables in PHP&lt;/strong&gt;&lt;/a&gt;), except for a few minor differences, which you’ll learn about in this article. You’ll also learn &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html#declaring_properties&quot; rel=&quot;tag&quot; title=&quot;declaring properties&quot;&gt;how to declare and invoke properties&lt;/a&gt;&lt;/strong&gt; and&lt;strong&gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html#declaring_property_scopes&quot; rel=&quot;tag&quot; title=&quot;declaring property scopes&quot;&gt;how to restrict access using property scopes&lt;/a&gt;.&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h1 id=&quot;declaring_properties&quot;&gt;
Declaring Properties&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The rules regarding property declaration are quite similar to those in place for variable declaration (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot; rel=&quot;tag&quot; title=&quot;Variables&quot;&gt;&lt;strong&gt;more about variables declaration in PHP&lt;/strong&gt;&lt;/a&gt;); essentially, there are none. Because PHP is a loosely typed language, properties don’t even necessarily need to be declared; they can simply be created and assigned simultaneously by a class object, although you’ll rarely want to do that.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
For introduction to classes and objects in PHP refer to my previous post &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;&lt;b&gt;Key&lt;/b&gt;&lt;strong&gt;&amp;nbsp;concepts of OOP in PHP&lt;/strong&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Instead, common practice is to declare properties at the beginning of the class. Optionally, you can assign them initial values at this time. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
public $name = &quot;John&quot;;&lt;br /&gt;
private $wage;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
In this example, the two properties, name and wage, are prefaced with a &lt;em&gt;&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html#declaring_property_scopes&quot; rel=&quot;tag&quot; title=&quot;declaring property scopes&quot;&gt;scope descriptor&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt; (public or private), a common practice when declaring properties. Once declared, each property can be used under the terms accorded to it by the &lt;em&gt;scope descriptor&lt;/em&gt;. If you don’t know what role scope plays in class properties, don’t worry, this topic is covered later in this article.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Invoking Properties&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Properties are referred to using the &lt;strong&gt;&lt;span style=&quot;color: black;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/strong&gt; operator and, unlike variables, are not prefaced with a dollar sign. Furthermore, because a property’s value typically is specific to a given object, it is correlated to that object like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;$object-&amp;gt;property&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
For example, the Employee class includes the properties &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;name, title, and wage&lt;/span&gt;&lt;/em&gt;. If you create an object of type Employee, you would refer to its properties like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$employee-&amp;gt;name&lt;br /&gt;
$employee-&amp;gt;title&lt;br /&gt;
$employee-&amp;gt;wage&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
When you refer to a property from within the class in which it is defined, it is still prefaced with the &lt;strong&gt;&lt;span style=&quot;color: black;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/strong&gt; operator, although instead of correlating it to the class name, you use the&lt;strong&gt; $this&lt;/strong&gt; keyword. &lt;strong&gt;$this&lt;/strong&gt; implies that you’re referring to the property residing in the same class in which the property is being accessed or manipulated. Therefore, if you were to create a method for setting the name property in the Employee class, it might look like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
function setName($name)&lt;br /&gt;
{&lt;br /&gt;
$this-&amp;gt;name = $name;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;h1 id=&quot;declaring_property_scopes&quot;&gt;
Property Scopes&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP supports five class property scopes: &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;public, private, protected, final,&lt;/span&gt;&lt;/em&gt; and &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;static&lt;/em&gt;&lt;/span&gt;. The first four are introduced in this article, and the static scope is introduced in the article “&lt;strong&gt;More about Object Oriented PHP&lt;/strong&gt;”. &lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Public&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
You can declare properties in the public scope by prefacing the property with the keyword public. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
public $name;&lt;br /&gt;
// Other property and method declarations follow...&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Public properties can then be accessed and manipulated directly via the corresponding object, like so:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$employee = new Employee();&lt;br /&gt;
$employee-&amp;gt;name = &quot;Mary Swanson&quot;;&lt;br /&gt;
$name = $employee-&amp;gt;name;&lt;br /&gt;
echo &quot;New employee: $name&quot;;&lt;/div&gt;
&lt;br /&gt;
Executing this code produces the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
New employee: Mary Swanson&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Although this might seem like a logical means for maintaining class properties, public properties are actually generally considered taboo to OOP, and for good reason. The reason for shunning such an implementation is that such direct access robs the class of a convenient means for enforcing any sort of data validation. For example, nothing would prevent the user from assigning name like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$employee-&amp;gt;name = &quot;12345&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This is certainly not the kind of input you are expecting. To prevent such occurrences, two solutions are available. One solution involves encapsulating the data within the object, making it available only via a series of interfaces, known as &lt;em&gt;&lt;strong&gt;public methods&lt;/strong&gt;&lt;/em&gt;. Data encapsulated in this way is said to be private in scope. The second recommended solution involves the use of properties and is actually quite similar to the first solution, although it is a tad more convenient in most cases.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Private&lt;/span&gt;&lt;/h1&gt;
Private properties are only accessible from within the class in which they are defined. An example follows:&lt;br /&gt;
&lt;br /&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
private $name;&lt;br /&gt;
private $telephone;&lt;br /&gt;
}&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Properties designated as private are not directly accessible by an &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;instantiated object&lt;/a&gt;&lt;/strong&gt;, nor are they available to child classes (&lt;strong&gt;more about child classes in OOP model of PHP&lt;/strong&gt;). If you want to make these properties available to child classes, consider using the protected scope instead, introduced next. Note that private properties must be accessed via publicly exposed interfaces, which satisfies one of OOP’s main tenets: encapsulation. Consider the following example, in which a private property is manipulated by a public method:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
private $name;&lt;br /&gt;
public function setName($name) {&lt;br /&gt;
$this-&amp;gt;name = $name;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
$employee = new Employee;&lt;br /&gt;
$employee-&amp;gt;setName(&quot;Mary&quot;);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Encapsulating the management of such properties within a method enables the developer to maintain tight control over how that property is set. For example, you could add to the &lt;strong&gt;setName() method’s &lt;/strong&gt;capabilities to validate that the name is set to solely alphabetical characters and to ensure that it isn’t blank. This strategy is much more reliable than leaving it to the end user to provide valid information.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Protected&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Just like functions often require variables intended for use only within the function, classes can include properties used for solely internal purposes. Such properties are deemed protected and are prefaced accordingly. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
protected $wage;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Protected properties are also made available to inherited classes for access and manipulation, a trait not shared by private properties. Any attempt by an object to access a protected property will result in a fatal error. Therefore, if you plan on extending the class, you should use protected properties in lieu of private properties.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Final&lt;/span&gt;&lt;/h1&gt;
Marking a property as final prevents it from being overridden by a subclass. A finalized property is declared like so:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
final $ssn;&lt;br /&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;Key Concepts of Object Oriented PHP&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-2.html&quot;&gt;Defining Class Properties :: continued&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/5419948957095425482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-properties-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5419948957095425482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5419948957095425482'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/class-properties-1.html' title='Defining Class Properties in PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-4531466729715114904</id><published>2013-07-23T23:29:00.000+05:30</published><updated>2013-08-01T23:54:27.320+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="classes"/><category scheme="http://www.blogger.com/atom/ns#" term="Key concepts of OOP in PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Key concepts of OOP in PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
In the previous post on &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/benefits-of-object-oriented-programming.html&quot;&gt;Benefits of Object-Oriented Programming&lt;/a&gt;&lt;/strong&gt; I focused three of OOP’s foundational concepts: &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;encapsulation, inheritance, and polymorphism.&lt;/em&gt; Now it is time to tell you what it really means in the world of PHP.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Classes&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Our everyday environment consists of countless entities: plants, people, vehicles, food...I could go on for hours just listing them. Each entity is defined by a particular &lt;em&gt;set of characteristics and behaviors&lt;/em&gt; that ultimately serves to define&lt;a href=&quot;http://lh4.ggpht.com/-YP9jRR4THDU/UflQbG9mZHI/AAAAAAAAAH0/TuL37C-hTXg/s1600-h/a29.png&quot;&gt;&lt;img align=&quot;right&quot; alt=&quot;class in php&quot; height=&quot;240&quot; src=&quot;http://lh3.ggpht.com/-ux04X08oX90/UflQei-_z2I/AAAAAAAAAH8/3mS8wnCwY68/a_thumb27.png?imgmax=800&quot; style=&quot;display: inline; float: right; margin: 5px;&quot; title=&quot;class in php&quot; width=&quot;167&quot; /&gt;&lt;/a&gt; the entity for what it is. For example, a vehicle might be defined as having &lt;em&gt;characteristics&lt;/em&gt; such as color, number of tires, make, model, and capacity, and having &lt;em&gt;behaviors&lt;/em&gt; such as stop, go, turn, and honk horn.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;&lt;strong&gt;In the vocabulary of OOP, such an embodiment of an entity’s defining attributes and behaviors is known as a &lt;u&gt;Class&lt;/u&gt;. The set of characteristics and behaviors are better known in OOP as &lt;u&gt;properties and methods&lt;/u&gt;, respectively&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Classes are intended to represent those real-life items that you’d like to manipulate within an application. For example, if you want to create an application for managing a public library, you’d probably want to include classes representing books, magazines, employees, special events, patrons, and anything else that would require oversight. &lt;em&gt;Each of these entities embodies a certain set of characteristics and behaviors, better known in OOP as properties and methods, respectively, that define the entity as what it is.&lt;/em&gt; PHP’s generalized class creation syntax follows:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;class ClassName&lt;br /&gt;{&lt;br /&gt;// Property declarations defined here&lt;br /&gt;// Method declarations defined here&lt;br /&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&lt;u&gt;Listing 6-1. Class Creation - a class representing employees&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class Employee&lt;br /&gt;
{&lt;br /&gt;
private $name;&lt;br /&gt;
private $title;&lt;br /&gt;
protected $wage;&lt;br /&gt;
protected function clockIn() {&lt;br /&gt;
echo &quot;Member $this-&amp;gt;name clocked in at &quot;.date(&quot;h:i:s&quot;);&lt;br /&gt;
}&lt;br /&gt;
protected function clockOut() {&lt;br /&gt;
echo &quot;Member $this-&amp;gt;name clocked out at &quot;.date(&quot;h:i:s&quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Titled Employee, this class defines three properties: &lt;span style=&quot;color: black;&quot;&gt;&lt;em&gt;name&lt;/em&gt;, &lt;em&gt;title&lt;/em&gt;, and &lt;em&gt;wage&lt;/em&gt;&lt;/span&gt;, in addition to two methods, &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;clockIn&lt;/span&gt;&lt;/em&gt; and &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;clockOut&lt;/span&gt;&lt;/em&gt;. Don’t worry if you’re not familiar with some of the syntax; it will become clear later in the articles that follows.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; While no official PHP code conventions exist, consider following the PHP Extension and Application Repository (PEAR) guidelines when creating your classes. You can learn more about these conventions at &lt;strong&gt;&lt;a href=&quot;http://pear.php.net/&quot; target=&quot;_blank&quot;&gt;pear website&lt;/a&gt;&lt;/strong&gt;.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1&gt;
Objects&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;A class provides a basis from which you can create specific instances of the entity the class models, better known as &lt;strong&gt;objects&lt;/strong&gt;&lt;/em&gt;. For example, an employee management application may include an Employee class. You can then call upon this class to create and maintain specific instances, Sally and Jim, for example.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The practice of creating objects based on predefined classes is often referred to as &lt;u&gt;class instantiation&lt;/u&gt;.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
Objects are created using the new keyword, like this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;$object_name = new class_name()&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
For Example:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
$employee = new Employee();&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Once the object is created, all of the characteristics and behaviors defined within the class are made available to the newly &lt;em&gt;instantiated object&lt;/em&gt;. Exactly how this is accomplished is revealed in the given articles:-&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/key-concepts-of-oop-in-php.html#constants&quot;&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;Defining Constants in&amp;nbsp;&lt;/em&gt;&lt;/strong&gt;&lt;strong&gt;&lt;em&gt;Object Oriented PHP&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html&quot;&gt;Defining Class Properties in PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-methods.html&quot;&gt;Defining Class Methods in PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/using-constructors-and-destructors.html&quot;&gt;Using Constructors and Destructors in PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/more-components-of-object-oriented-php.html&quot;&gt;More Components of Object Oriented PHP&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style=&quot;text-align: left;&quot;&gt;&lt;em&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/helper-functions-in-object-oriented-php.html&quot;&gt;Helper Functions in&amp;nbsp;&lt;strong style=&quot;font-style: normal;&quot;&gt;&lt;em&gt;Object Oriented PHP&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;strong&gt;&lt;u&gt;Classes&lt;/u&gt; form the structure of data and actions and use that information to build &lt;u&gt;objects&lt;/u&gt;.&lt;/strong&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 id=&quot;constants&quot;&gt;
Constants&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
You can define constants, or values that are not intended to change, within a class. These values will remain unchanged throughout the lifetime of any object instantiated from that class. Class constants are created like so:&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;const NAME = &#39;VALUE&#39;;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
For example, suppose you create a math-related class that contains a number of methods defining mathematical functions, in addition to numerous constants:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
class mathFunctions&lt;br /&gt;
{&lt;br /&gt;
const PI = &#39;3.14159265&#39;;&lt;br /&gt;
const E = &#39;2.7182818284&#39;;&lt;br /&gt;
const EULER = &#39;0.5772156649&#39;;&lt;br /&gt;
// Define other constants and methods here...&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Class constants can then be called like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
echo mathFunctions::PI;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/benefits-of-object-oriented-programming.html&quot;&gt;Benefits of OOP&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/class-properties-1.html&quot;&gt;Definining Class Properties&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/4531466729715114904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4531466729715114904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4531466729715114904'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html' title='Key concepts of OOP in PHP'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-ux04X08oX90/UflQei-_z2I/AAAAAAAAAH8/3mS8wnCwY68/s72-c/a_thumb27.png?imgmax=800" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-6687675363204401295</id><published>2013-07-22T23:25:00.000+05:30</published><updated>2013-08-01T01:24:55.564+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Encapsulation"/><category scheme="http://www.blogger.com/atom/ns#" term="inheritance"/><category scheme="http://www.blogger.com/atom/ns#" term="object"/><category scheme="http://www.blogger.com/atom/ns#" term="object oriented php"/><category scheme="http://www.blogger.com/atom/ns#" term="OOP"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="polymorphism"/><title type='text'>Benefits of Object-Oriented Programming</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Although PHP did not start out as an &lt;em&gt;object-oriented language&lt;/em&gt;, over the years a great deal of effort has been put into adding many of the object-oriented features found in other languages. This article and the following aim to introduce these features. Before doing so, let’s consider the advantages of the OOP development model.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;a href=&quot;http://lh4.ggpht.com/-sohuo8nIq5U/UflPYNkuJXI/AAAAAAAAAHk/LEqk1RzXjEo/s1600-h/Capture11.png&quot;&gt;&lt;img alt=&quot;Object Oriented PHP&quot; height=&quot;174&quot; src=&quot;http://lh3.ggpht.com/-8prQaeF3ZTY/UflPdnJLrpI/AAAAAAAAAHs/DSHdiut510w/Capture_thumb9.png?imgmax=800&quot; style=&quot;display: block; float: none; margin-left: auto; margin-right: auto;&quot; title=&quot;Object Oriented PHP&quot; width=&quot;692&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Just a thought:&lt;/strong&gt; If you got any OOP history before PHP this post is going to be a tom’s task for you- very easy. But if you are new to OOP concept and you got confused at any point, don’t get disheartened because as you will proceed to your journey with OOP features of PHP you will realize what I want to say here. Because&amp;nbsp; things can very easily be understood in the field rather than the theory.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1&gt;
The Benefits of OOP&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The birth of &lt;em&gt;object-oriented programming&lt;/em&gt; represented a major paradigm shift in development strategy, &lt;strong&gt;refocusing attention on an application’s data rather than its logic&lt;/strong&gt;. To put it another way, OOP shifts the focus from a program’s procedural events toward the real-life entities it is intended to model. The result is an application that closely resembles the world around us.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
This section examines three of OOP’s foundational concepts: &lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;encapsulation, inheritance, and polymorphism&lt;/span&gt;&lt;/em&gt;. Together, these three ideals form the basis for the most powerful programming model yet devised.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Encapsulation&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Programmers enjoy taking things apart and learning how all of the little pieces work together. Although gratifying, attaining such in-depth knowledge of an item’s inner workings isn’t a requirement. For example, millions of people use a computer every day, yet few know how it actually works. The same idea applies to automobiles, microwaves, and any number of other items. We can get away with such ignorance through the use of interfaces. For example, you know that turning the radio dial allows you to change radio stations; never mind the fact that what you’re actually doing is telling the radio to listen to the signal transmitted at a particular frequency, a feat accomplished using a demodulator. Failing to understand this process does not prevent you from using the radio because the interface gracefully hides such details. The practice of separating the user from the true inner workings of an application through well-known interfaces is known as encapsulation.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Object-oriented programming promotes the same notion of hiding the inner workings of the application by publishing well-defined &lt;em&gt;interfaces&lt;/em&gt; from which each application component can be accessed. Rather than get bogged down in the gory details, OOP-minded developers design each application component so that it is independent from the others, which not only encourages reuse but also enables the developer to assemble components like a puzzle rather than tightly lash, or couple, them together. These components are known as &lt;strong&gt;OBJECTS&lt;/strong&gt;, and objects are created from a template known as a &lt;strong&gt;CLASS&lt;/strong&gt;, which specifies what sorts of data the object might contain and the behavior one would expect. This strategy offers several advantages:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
The developer can change the application implementation without affecting the object user because the user’s only interaction with the object is via its interface.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
The potential for user error is reduced because of the control exercised over the user’s interaction with the application.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Inheritance&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The many objects constituting our environment can be modeled using a fairly well-defined set of rules. For instance, all employees share a common set of characteristics: name, employee ID, and wage. However, there are many different types of employees: clerks, supervisors, cashiers, and chief executive officers, among others, each of which likely possesses some superset of those characteristics defined by the generic employee definition. In object-oriented terms, these various employee types inherit the general employee definition, including all of the characteristics and behaviors that contribute to this definition. In turn, each of these specific employee types could be &lt;em&gt;&lt;strong&gt;inherited&lt;/strong&gt;&lt;/em&gt; by yet another more specific type. For example, the Clerk type might be &lt;em&gt;inherited&lt;/em&gt; by a day clerk and a night clerk, each of which &lt;em&gt;inherits&lt;/em&gt; all traits specified by both the employee definition and the clerk definition. Building on this idea, you could then later create a Human class, and then make the Employee class a &lt;em&gt;subclass&lt;/em&gt; of Human.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;&lt;strong&gt;The effect would be that the Employee class and all of its derived classes (Clerk, Cashier, Executive, etc.) would immediately inherit all characteristics and behaviors defined by Human.&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The object-oriented development methodology places great stock in the concept of inheritance. This strategy promotes code reusability because it assumes that one will be able to use well-designed classes (i.e., classes that are sufficiently abstract to allow for reuse) within numerous applications.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Polymorphism&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Polymorphism, &lt;em&gt;a term originating from the Greek language that means “having multiple forms”&lt;/em&gt;, defines OOP’s ability to redefine, or morph, a class’s characteristic or behavior depending upon the context in which it is used. Returning to the example, suppose that a behavior titled clockIn was included within the employee definition. For employees of class Clerk, this behavior might involve actually using a time clock to timestamp a card. For other types of employees, Programmer for instance, clocking in might involve signing on to the corporate network. Although both classes derive this behavior from the Employee class, the actual implementation of each is dependent upon the context in which “clocking in” is implemented. This is the power of polymorphism.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
These three key OOP concepts (encapsulation, inheritance, and polymorphism) are further introduced as they apply to PHP through the next articles.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note: &lt;/strong&gt;&lt;em&gt;&lt;span style=&quot;color: black;&quot;&gt;While it may be daunting at first, OOP actually provides an easier approach to dealing with data than the procedural programming&lt;/span&gt;&lt;/em&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/key-concepts-of-oop-in-php.html&quot;&gt;Move on to Key concepts of OOP in PHP&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/6687675363204401295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/benefits-of-object-oriented-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6687675363204401295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6687675363204401295'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/benefits-of-object-oriented-programming.html' title='Benefits of Object-Oriented Programming'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-8prQaeF3ZTY/UflPdnJLrpI/AAAAAAAAAHs/DSHdiut510w/s72-c/Capture_thumb9.png?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-8091158011322316932</id><published>2013-07-18T14:46:00.000+05:30</published><updated>2013-07-21T14:49:53.440+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="adding array elements"/><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array_chunk()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_rand()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_sum()"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="random set of keys"/><category scheme="http://www.blogger.com/atom/ns#" term="shuffle()"/><category scheme="http://www.blogger.com/atom/ns#" term="shuffling array elements"/><category scheme="http://www.blogger.com/atom/ns#" term="subdividing array elements"/><title type='text'>Other Useful Array Functions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This article introduces a number of array (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;&lt;b&gt;what is an array&lt;/b&gt;&lt;/a&gt;) functions (&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;&lt;b&gt;more about functions in php&lt;/b&gt;&lt;/a&gt;) that perhaps don’t easily fall into one of the prior articles but are nonetheless quite useful.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;text-align: justify;&quot;&gt;
Returning a Random Set of Keys&lt;/h1&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The array_rand() function will return a random number of keys found in an array. Its prototype follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed array_rand(array array [, int num_entries])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If you omit the optional num_entries parameter, only one random value will be returned. You can tweak the number of returned random values by setting num_entries accordingly. An example follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_code&quot; style=&quot;text-align: justify;&quot;&gt;
$states = array(&quot;Ohio&quot; =&amp;gt; &quot;Columbus&quot;, &quot;Iowa&quot; =&amp;gt; &quot;Des Moines&quot;,&lt;br /&gt;
&quot;Arizona&quot; =&amp;gt; &quot;Phoenix&quot;);&lt;br /&gt;
$randomStates = array_rand($states, 2);&lt;br /&gt;
print_r($randomStates);&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This returns the following (your output may vary):&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_output&quot; style=&quot;text-align: justify;&quot;&gt;
Array ( [0] =&amp;gt; Arizona [1] =&amp;gt; Ohio )&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;&lt;/h1&gt;
&lt;h1 style=&quot;text-align: justify;&quot;&gt;
Shuffling Array Elements&lt;/h1&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The shuffle() function randomly reorders an array. Its prototype follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;void shuffle(array input_array)&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Consider an array containing values representing playing cards:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_code&quot; style=&quot;text-align: justify;&quot;&gt;
$cards = array(&quot;jh&quot;, &quot;js&quot;, &quot;jd&quot;, &quot;jc&quot;, &quot;qh&quot;, &quot;qs&quot;, &quot;qd&quot;, &quot;qc&quot;,&lt;br /&gt;
&quot;kh&quot;, &quot;ks&quot;, &quot;kd&quot;, &quot;kc&quot;, &quot;ah&quot;, &quot;as&quot;, &quot;ad&quot;, &quot;ac&quot;);&lt;br /&gt;
shuffle($cards);&lt;br /&gt;
print_r($positions);&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This returns something along the lines of the following (your results will vary because of the shuffle):&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_output&quot; style=&quot;text-align: justify;&quot;&gt;
Array ( [0] =&amp;gt; js [1] =&amp;gt; ks [2] =&amp;gt; kh [3] =&amp;gt; jd&lt;br /&gt;
[4] =&amp;gt; ad [5] =&amp;gt; qd [6] =&amp;gt; qc [7] =&amp;gt; ah&lt;br /&gt;
[8] =&amp;gt; kc [9] =&amp;gt; qh [10] =&amp;gt; kd [11] =&amp;gt; as&lt;br /&gt;
[12] =&amp;gt; ac [13] =&amp;gt; jc [14] =&amp;gt; jh [15] =&amp;gt; qs )&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;text-align: justify;&quot;&gt;
Adding Array Values&lt;/h1&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The array_sum() function adds all the values of input_array together, returning the final sum. Its prototype follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;mixed array_sum(array array)&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If other data types (a string, for example) are found in the array, they will be ignored. An example follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_code&quot; style=&quot;text-align: justify;&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$grades = array(42, &quot;hello&quot;, 42);&lt;br /&gt;
$total = array_sum($grades);&lt;br /&gt;
print $total;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_output&quot; style=&quot;text-align: justify;&quot;&gt;
84&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;text-align: justify;&quot;&gt;
Subdividing an Array&lt;/h1&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The array_chunk() function breaks input_array into a multidimensional array that includes several smaller arrays consisting of size elements. Its prototype follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_chunk(array array, int size [, boolean preserve_keys])&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If the input_array can’t be evenly divided by size, the last array will consist of fewer than size elements. Enabling the optional parameter preserve_keys will preserve each value’s corresponding key. Omitting or disabling this parameter results in numerical indexing starting from zero for each array. An example follows:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_code&quot; style=&quot;text-align: justify;&quot;&gt;
$cards = array(&quot;jh&quot;, &quot;js&quot;, &quot;jd&quot;, &quot;jc&quot;, &quot;qh&quot;, &quot;qs&quot;, &quot;qd&quot;, &quot;qc&quot;,&lt;br /&gt;
&quot;kh&quot;, &quot;ks&quot;, &quot;kd&quot;, &quot;kc&quot;, &quot;ah&quot;, &quot;as&quot;, &quot;ad&quot;, &quot;ac&quot;);&lt;br /&gt;
// shuffle the cards&lt;br /&gt;
shuffle($cards);&lt;br /&gt;
// Use array_chunk() to divide the cards into four equal &quot;hands&quot;&lt;br /&gt;
$hands = array_chunk($cards, 4);&lt;br /&gt;
print_r($hands);&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This returns the following (your results will vary because of the shuffle):&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div id=&quot;php_output&quot; style=&quot;text-align: justify;&quot;&gt;
Array ( [0] =&amp;gt; Array ( [0] =&amp;gt; jc [1] =&amp;gt; ks [2] =&amp;gt; js [3] =&amp;gt; qd )&lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; kh [1] =&amp;gt; qh [2] =&amp;gt; jd [3] =&amp;gt; kd )&lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; jh [1] =&amp;gt; kc [2] =&amp;gt; ac [3] =&amp;gt; as )&lt;br /&gt;
[3] =&amp;gt; Array ( [0] =&amp;gt; ad [1] =&amp;gt; ah [2] =&amp;gt; qc [3] =&amp;gt; qs ) )&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/splicing-and-dissecting-arrays.html&quot;&gt;Splicing and Dissectiong Arrays&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/8091158011322316932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/useful-array-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8091158011322316932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/8091158011322316932'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/useful-array-functions.html' title='Other Useful Array Functions'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-1675221749624633631</id><published>2013-07-17T14:12:00.000+05:30</published><updated>2013-07-21T14:25:43.987+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array differences"/><category scheme="http://www.blogger.com/atom/ns#" term="array intersections"/><category scheme="http://www.blogger.com/atom/ns#" term="array_diff()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_diff_assoc()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_intersect()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_intersect_assoc()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_splice()"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="splicnig an array"/><title type='text'>Splicing and Dissecting Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Splicing an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_splice() function removes all elements of an array (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;what is an array&lt;/a&gt;) found within a specified range, returning those removed elements in the form of an array. Its prototype follows:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;array array_splice(array array, int offset [, int length [, array replacement]])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
A positive offset value will cause the splice to begin that many positions from the beginning of the array, while a negative offset will start the splice that many positions from the end of the array. If the optional length parameter is omitted, all elements from the offset position to the conclusion of the &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot;&gt;array&lt;/a&gt; will be removed. If length is provided and is positive, the splice will end at offset + length position from the beginning of the array. Conversely, if length is provided and is negative, the splice will end at count(input_array) – length position from the end of the array. An example follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Alabama&quot;, &quot;Alaska&quot;, &quot;Arizona&quot;, &quot;Arkansas&quot;,&lt;br /&gt;
&quot;California&quot;, &quot;Connecticut&quot;);&lt;br /&gt;
$subset = array_splice($states, 4);&lt;br /&gt;
print_r($states);&lt;br /&gt;
print_r($subset);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This produces the following (&lt;em&gt;formatted for readability&lt;/em&gt;):&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Alabama [1] =&amp;gt; Alaska [2] =&amp;gt; Arizona [3] =&amp;gt; Arkansas )&lt;br /&gt;
Array ( [0] =&amp;gt; California [1] =&amp;gt; Connecticut )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You can use the optional parameter replacement to specify an array that will replace the target segment. An example follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Alabama&quot;, &quot;Alaska&quot;, &quot;Arizona&quot;, &quot;Arkansas&quot;,&lt;br /&gt;
&quot;California&quot;, &quot;Connecticut&quot;);&lt;br /&gt;
$subset = array_splice($states, 2, -1, array(&quot;New York&quot;, &quot;Florida&quot;));&lt;br /&gt;
print_r($states);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Alabama [1] =&amp;gt; Alaska [2] =&amp;gt; New York&lt;br /&gt;
[3] =&amp;gt; Florida [4] =&amp;gt; Connecticut )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Calculating an Array Intersection&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_intersect() function returns a key-preserved array consisting only of those values present in the first array that are also present in each of the other input arrays. Its prototype follows:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_intersect(array array1, array array2 [, arrayN])&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The following example will return all states found in the $array1 that also appear in $array2 and $array3:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$array1 = array(&quot;OH&quot;, &quot;CA&quot;, &quot;NY&quot;, &quot;HI&quot;, &quot;CT&quot;);&lt;br /&gt;
$array2 = array(&quot;OH&quot;, &quot;CA&quot;, &quot;HI&quot;, &quot;NY&quot;, &quot;IA&quot;);&lt;br /&gt;
$array3 = array(&quot;TX&quot;, &quot;MD&quot;, &quot;NE&quot;, &quot;OH&quot;, &quot;HI&quot;);&lt;br /&gt;
$intersection = array_intersect($array1, $array2, $array3);&lt;br /&gt;
print_r($intersection);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; OH [3] =&amp;gt; HI )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; array_intersect() considers two items to be equal only if they also share the same data type.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Introduced in PHP 5.1.0, the array_intersect_key() function will return keys located in an array that are located in any of the other provided arrays. The function’s prototype is identical to array_intersect(). Likewise, the array_intersect_ukey() function allows you to compare the keys of multiple arrays with the comparison algorithm determined by a user-defined function. Consult the PHP manual for more information.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Calculating Associative Array Intersections&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function array_intersect_assoc() operates identically to array_intersect(), except that it also considers array keys in the comparison. Therefore, only key/value pairs located in the first array that are also found in all other input arrays will be returned in the resulting array. Its prototype follows:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_intersect_assoc(array array1, array array2 [, arrayN])&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The following example returns an array consisting of all key/value pairs found in $array1 that also appear in $array2 and $array3:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$array1 = array(&quot;OH&quot; =&amp;gt; &quot;Ohio&quot;, &quot;CA&quot; =&amp;gt; &quot;California&quot;, &quot;HI&quot; =&amp;gt; &quot;Hawaii&quot;);&lt;br /&gt;
$array2 = array(&quot;50&quot; =&amp;gt; &quot;Hawaii&quot;, &quot;CA&quot; =&amp;gt; &quot;California&quot;, &quot;OH&quot; =&amp;gt; &quot;Ohio&quot;);&lt;br /&gt;
$array3 = array(&quot;TX&quot; =&amp;gt; &quot;Texas&quot;, &quot;MD&quot; =&amp;gt; &quot;Maryland&quot;, &quot;OH&quot; =&amp;gt; &quot;Ohio&quot;);&lt;br /&gt;
$intersection = array_intersect_assoc($array1, $array2, $array3);&lt;br /&gt;
print_r($intersection);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [OH] =&amp;gt; Ohio )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Hawaii was not returned because the corresponding key in $array2 is 50 rather than HI (as is the case in the other two arrays).&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Calculating Array Differences&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Essentially the opposite of array_intersect(), the function array_diff() returns those values located in the first array that are not located in any of the subsequent arrays:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_diff(array array1, array array2 [, arrayN])&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
An example follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$array1 = array(&quot;OH&quot;, &quot;CA&quot;, &quot;NY&quot;, &quot;HI&quot;, &quot;CT&quot;);&lt;br /&gt;
$array2 = array(&quot;OH&quot;, &quot;CA&quot;, &quot;HI&quot;, &quot;NY&quot;, &quot;IA&quot;);&lt;br /&gt;
$array3 = array(&quot;TX&quot;, &quot;MD&quot;, &quot;NE&quot;, &quot;OH&quot;, &quot;HI&quot;);&lt;br /&gt;
$diff = array_diff($array1, $array2, $array3);&lt;br /&gt;
print_r($intersection);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; CT )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If you’d like to compare array values using a user-defined function (&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;more about functions in php&lt;/a&gt;) , check out the array_udiff() function, introduced in PHP 5.0.2.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Introduced in PHP 5.1.0, the array_diff_key() function will return keys located in an array that are not located in any of the other provided arrays. The function’s prototype is identical to array_diff(). Likewise, the array_diff_ukey() function allows you to compare the keys of multiple arrays with the comparison algorithm determined by a user-defined function. Consult the PHP manual for more information.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Calculating Associative Array Differences&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function array_diff_assoc() operates identically to array_diff(), except that it also considers array keys in the comparison. Therefore, only key/value pairs located in the first array but not appearing in any of the other input arrays will be returned in the result array. Its prototype follows:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_diff_assoc(array array1, array array2 [, array arrayN])&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The following example only returns &quot;HI&quot; =&amp;gt; &quot;Hawaii&quot; because this particular key/value appears in $array1 but doesn’t appear in $array2 or $array3:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$array1 = array(&quot;OH&quot; =&amp;gt; &quot;Ohio&quot;, &quot;CA&quot; =&amp;gt; &quot;California&quot;, &quot;HI&quot; =&amp;gt; &quot;Hawaii&quot;);&lt;br /&gt;
$array2 = array(&quot;50&quot; =&amp;gt; &quot;Hawaii&quot;, &quot;CA&quot; =&amp;gt; &quot;California&quot;, &quot;OH&quot; =&amp;gt; &quot;Ohio&quot;);&lt;br /&gt;
$array3 = array(&quot;TX&quot; =&amp;gt; &quot;Texas&quot;, &quot;MD&quot; =&amp;gt; &quot;Maryland&quot;, &quot;KS&quot; =&amp;gt; &quot;Kansas&quot;);&lt;br /&gt;
$diff = array_diff_assoc($array1, $array2, $array3);&lt;br /&gt;
print_r($diff);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [HI] =&amp;gt; Hawaii )
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Introduced in PHP 5.0, the array_udiff_assoc(), array_udiff_uassoc(), and array_diff_uassoc() functions are all capable of comparing the differences of arrays in a variety of manners using user-defined functions. Consult the PHP manual for more information.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/merging-and-slicing-arrays.html&quot;&gt;Merging and Slicing Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/other-array-functions.html&quot;&gt;Other Array Functions&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/1675221749624633631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/splicing-and-dissecting-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1675221749624633631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1675221749624633631'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/splicing-and-dissecting-arrays.html' title='Splicing and Dissecting Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-4891920243716335847</id><published>2013-07-16T00:29:00.000+05:30</published><updated>2013-07-16T00:29:26.404+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Usefull Links</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&amp;nbsp;Here are some usefull links, you might need. Keep it handy.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Official Sites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/&quot;&gt;http://www.php.net/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/&quot;&gt;http://www.zend.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;Documentat&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Documentation&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/docs.php&quot;&gt;http://www.php.net/docs.php&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPPortals&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Portals and Communities&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpbuilder.com/&quot;&gt;http://www.phpbuilder.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpfreaks.com/&quot;&gt;http://www.phpfreaks.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpkitchen.com/&quot;&gt;http://www.phpkitchen.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://php.nilostep.com/&quot;&gt;http://php.nilostep.com/&lt;/a&gt; PHP Class Libraries and Application Frameworks  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/&quot;&gt;http://www.zend.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.weberdev.com/&quot;&gt;http://www.weberdev.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpcomplete.com/&quot;&gt;http://www.phpcomplete.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://codewalkers.com/&quot;&gt;http://codewalkers.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpwelt.de/&quot;&gt;http://www.phpwelt.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dynamic-webpages.de/&quot;&gt;http://www.dynamic-webpages.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.isernese.de/&quot;&gt;http://www.isernese.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php-homepage.de/&quot;&gt;http://www.php-homepage.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php-resource.de/&quot;&gt;http://www.php-resource.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpindex.com/&quot;&gt;http://www.phpindex.com/&lt;/a&gt; (french)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php-corner.com/&quot;&gt;http://www.php-corner.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPNewsand&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP News and Blogs&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/zend/week/&quot;&gt;http://www.zend.com/zend/week/&lt;/a&gt; PHP Weekly Summary  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://phplens.com/phpeverywhere/&quot;&gt;http://phplens.com/phpeverywhere/&lt;/a&gt; PHP Everywhere by John Lim  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.blueshoes.org/en/blog/&quot;&gt;http://www.blueshoes.org/en/blog/&lt;/a&gt; PHP Blog by Fabian Kessler  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.sitepoint.com/blogs/category/php/&quot;&gt;http://www.sitepoint.com/blogs/category/php/&lt;/a&gt; Sitepoint PHP Blog by various writers  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.procata.com/blog/&quot;&gt;http://www.procata.com/blog/&lt;/a&gt; PHP Advocacy and Best Practices  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://shiflett.org/&quot;&gt;http://shiflett.org/&lt;/a&gt; PHP Blog by Chris Shiflett  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://blogs.phparch.com/mt/&quot;&gt;http://blogs.phparch.com/mt/&lt;/a&gt; Marco Tabini&#39;s Weblog  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/&quot;&gt;http://www.php.net/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;Forums&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2&gt;
Forums&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://forums.devnetwork.net/&quot;&gt;http://forums.devnetwork.net/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPTutoria&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Tutorials and Articles&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.devshed.com/Server_Side/PHP/&quot;&gt;http://www.devshed.com/Server_Side/PHP/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.onlamp.com/php/&quot;&gt;http://www.onlamp.com/php/&lt;/a&gt; (oreilly)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/&quot;&gt;http://www.zend.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPCodeArc&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Code Archives&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.hotscripts.com/PHP/?RID=2936&quot;&gt;http://www.hotscripts.com/PHP/?RID=2936&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://php.foundries.sourceforge.net/&quot;&gt;http://php.foundries.sourceforge.net/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://php.resourceindex.com/&quot;&gt;http://php.resourceindex.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpclasses.org/&quot;&gt;http://www.phpclasses.org/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.webmasterengine.com/phpcgiscripts.html&quot;&gt;http://www.webmasterengine.com/phpcgiscripts.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://px.sklar.com/&quot;&gt;http://px.sklar.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.evilwalrus.com/&quot;&gt;http://www.evilwalrus.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://dev.nexen.net/scripts/index.php?langue=en&quot;&gt;http://dev.nexen.net/scripts/index.php?langue=en&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;Editorsand&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Editors and IDE&#39;s&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.waterproof.fr/&quot;&gt;http://www.waterproof.fr/&lt;/a&gt; PHPEdit  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/store/products/zend-studio/index.php&quot;&gt;http://www.zend.com/store/products/zend-studio/index.php&lt;/a&gt; Zend Studio  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.maguma.com/&quot;&gt;http://www.maguma.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.eclipse.org/&quot;&gt;http://www.eclipse.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPHelpand&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Help and FAQs&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://php.faqts.com/&quot;&gt;http://php.faqts.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.alt-php-faq.org/&quot;&gt;http://www.alt-php-faq.org/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://faq-php.de/&quot;&gt;http://faq-php.de/&lt;/a&gt; de.comp.lang.php FAQ&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPMagazin&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Magazines&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phparch.com/&quot;&gt;http://www.phparch.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://phpj.com/&quot;&gt;http://phpj.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpmag.de/&quot;&gt;http://www.phpmag.de/&lt;/a&gt; (german)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zephpmag.com/&quot;&gt;http://www.zephpmag.com/&lt;/a&gt; (french)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPSetupTo&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Setup Tools for Linux&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://sourceforge.net/projects/foxserv&quot;&gt;http://sourceforge.net/projects/foxserv&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://sourceforge.net/projects/appserv&quot;&gt;http://sourceforge.net/projects/appserv&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPSetupTo&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Setup Tools for Windows&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://sourceforge.net/projects/foxserv&quot;&gt;http://sourceforge.net/projects/foxserv&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://sourceforge.net/projects/appserv&quot;&gt;http://sourceforge.net/projects/appserv&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php4win.com/&quot;&gt;http://www.php4win.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.firepages.com.au/dev4.htm&quot;&gt;http://www.firepages.com.au/dev4.htm&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.easyphp.org/&quot;&gt;http://www.easyphp.org/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.analogx.com/contents/download/network/phpconf.htm&quot;&gt;http://www.analogx.com/contents/download/network/phpconf.htm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPAcceler&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Accelerators (Code Cache)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://eaccelerator.net/&quot;&gt;http://eaccelerator.net/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/store/products/zend-accelerator.php&quot;&gt;http://www.zend.com/store/products/zend-accelerator.php&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/apc&quot;&gt;http://www.php.net/apc&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php-accelerator.co.uk/&quot;&gt;http://www.php-accelerator.co.uk/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPEncoder&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Encoder&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.ioncube.com/encoder/&quot;&gt;http://www.ioncube.com/encoder/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.zend.com/store/products/zend-safeguard-suite.php&quot;&gt;http://www.zend.com/store/products/zend-safeguard-suite.php&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.rssoftlab.com/phpenc.php4&quot;&gt;http://www.rssoftlab.com/phpenc.php4&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://sourceguardian.com/&quot;&gt;http://sourceguardian.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPDebugge&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Debugger&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://apd.communityconnect.com/&quot;&gt;http://apd.communityconnect.com/&lt;/a&gt; APD (Advanced PHP Debugger)  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pear.php.net/apd/&quot;&gt;http://pear.php.net/apd/&lt;/a&gt; PEAR website of APD  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/apd&quot;&gt;http://www.php.net/apd&lt;/a&gt; PHP Documentation of APD  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.wiki.cc/php/Apd&quot;&gt;http://www.wiki.cc/php/Apd&lt;/a&gt; Wiki for APD  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.linuxjournal.com/article/7213&quot;&gt;http://www.linuxjournal.com/article/7213&lt;/a&gt; Article about PHP Performance Profiling  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.schlossnagle.org/~george/talks/Profiling-phpworks-2004.pdf&quot;&gt;http://www.schlossnagle.org/~george/talks/Profiling-phpworks-2004.pdf&lt;/a&gt; Another article (PDF) by George Schlossnagle  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://dd.cron.ru/dbg/&quot;&gt;http://dd.cron.ru/dbg/&lt;/a&gt; dbg  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.xdebug.org/&quot;&gt;http://www.xdebug.org/&lt;/a&gt; Xdebug  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pear.php.net/package/benchmark/redirected&quot;&gt;http://pear.php.net/package/benchmark/redirected&lt;/a&gt; Benchmark (in PECL)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPDirecto&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Directories&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://dmoz.org/Computers/Programming/Languages/PHP/Scripts/&quot;&gt;http://dmoz.org/Computers/Programming/Languages/PHP/Scripts/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://dmoz.org/World/Deutsch/Computer/Programmieren/Internet/PHP/&quot;&gt;http://dmoz.org/World/Deutsch/Computer/Programmieren/Internet/PHP/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPMailing&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP Mailing Lists&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://p2p.wrox.com/&quot;&gt;http://p2p.wrox.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php.net/support.php&quot;&gt;http://www.php.net/support.php&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.php-center.de/php-de/&quot;&gt;http://www.php-center.de/php-de/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;PHPUserGro&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
PHP User Groups&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpug.de/&quot;&gt;http://www.phpug.de/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpug.ch/&quot;&gt;http://www.phpug.ch/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpug.at/&quot;&gt;http://www.phpug.at/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpgg.nl/&quot;&gt;http://www.phpgg.nl/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;Misc&quot;&gt;&lt;/a&gt; &lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Misc&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.devnetwork.net/&quot;&gt;http://www.devnetwork.net/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.evilwalrus.com/&quot;&gt;http://www.evilwalrus.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.phpadvisory.com/&quot;&gt;http://www.phpadvisory.com/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://freshmeat.net/browse/183/?topic_id=183&quot;&gt;http://freshmeat.net/browse/183/?topic_id=183&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Resource:&lt;/span&gt;&lt;/b&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;www.blueshoes.org&lt;/i&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/4891920243716335847/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/usefull-links.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4891920243716335847'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4891920243716335847'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/usefull-links.html' title='Usefull Links'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-9028665449909700392</id><published>2013-07-12T21:36:00.000+05:30</published><updated>2013-07-16T21:49:18.066+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array_combine()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_merge()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_merge_recursive()"/><category scheme="http://www.blogger.com/atom/ns#" term="array_slice()"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="merging and slicing arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Merging And Slicing Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This section introduces a number of functions&amp;nbsp;(&lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;more about functions in PHP&lt;/a&gt;&lt;/b&gt;) that are capable of performing somewhat more complex array-manipulation (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;&lt;b&gt;what is an array&lt;/b&gt;&lt;/a&gt;) tasks, such as combining and merging multiple arrays, extracting a cross-section of &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot;&gt;array&lt;/a&gt; elements, and comparing arrays.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Merging Arrays&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_merge() function merges arrays together, returning a single, unified array. The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_merge(array array1, array array2 [, array arrayN])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If an input array contains a string (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;what is a string&lt;/a&gt;&lt;/strong&gt;) key that already exists in the resulting array, that key/value pair&amp;nbsp;&lt;span style=&quot;text-align: left;&quot;&gt;will overwrite the previously existing entry. This behavior does not hold true for numerical keys, in which case the key/value pair will be appended to the array. An example follows:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$face = array(&quot;J&quot;, &quot;Q&quot;, &quot;K&quot;, &quot;A&quot;);&lt;br /&gt;
$numbered = array(&quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;);&lt;br /&gt;
$cards = array_merge($face, $numbered);&lt;br /&gt;
shuffle($cards);&lt;br /&gt;
print_r($cards);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns something along the lines of the following (your results will vary because of the shuffle):&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; 8 [1] =&amp;gt; 6 [2] =&amp;gt; K [3] =&amp;gt; Q [4] =&amp;gt; 9 [5] =&amp;gt; 5&lt;br /&gt;
[6] =&amp;gt; 3 [7] =&amp;gt; 2 [8] =&amp;gt; 7 [9] =&amp;gt; 4 [10] =&amp;gt; A [11] =&amp;gt; J )&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Recursively Appending Arrays&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_merge_recursive() function operates identically to array_merge(), joining two or more arrays together to form a single, unified array. The difference between the two functions lies in the way that this function behaves when a string key located in one of the input arrays already exists within the resulting array.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; array_merge() will simply overwrite the preexisting key/value pair, replacing it with the one found in the current input array, while array_merge_recursive() will instead merge the values together, forming a new array with the preexisting key as its name. &lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_merge_recursive(array array1, array array2 [, array arrayN])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$class1 = array(&quot;John&quot; =&amp;gt; 100, &quot;James&quot; =&amp;gt; 85);&lt;br /&gt;
$class2 = array(&quot;Micky&quot; =&amp;gt; 78, &quot;John&quot; =&amp;gt; 45);&lt;br /&gt;
$classScores = array_merge_recursive($class1, $class2);&lt;br /&gt;
print_r($classScores);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [John] =&amp;gt; Array ( [0] =&amp;gt; 100 [1] =&amp;gt; 45 ) [James] =&amp;gt; 85 [Micky] =&amp;gt; 78 )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Note that the key John now points to a numerically indexed array consisting of two scores.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Combining Two Arrays&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_combine() function produces a new array consisting of a submitted set of keys and corresponding values. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_combine(array keys, array values)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Both input arrays must be of equal size, and neither can be empty. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$abbreviations = array(&quot;AL&quot;, &quot;AK&quot;, &quot;AZ&quot;, &quot;AR&quot;);&lt;br /&gt;
$states = array(&quot;Alabama&quot;, &quot;Alaska&quot;, &quot;Arizona&quot;, &quot;Arkansas&quot;);&lt;br /&gt;
$stateMap = array_combine($abbreviations,$states);&lt;br /&gt;
print_r($stateMap);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [AL] =&amp;gt; Alabama [AK] =&amp;gt; Alaska [AZ] =&amp;gt; Arizona [AR] =&amp;gt; Arkansas )&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Slicing an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_slice() function returns a section of an array based on a starting and ending offset value. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;array array_slice(array array, int offset [, int length [, boolean preserve_keys]])&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
A positive offset value will cause the slice to begin offset positions from the beginning of the array, while a negative offset value will start the slice offset positions from the end of the array. If the optional &lt;em&gt;length parameter&lt;/em&gt; is omitted, the slice will start at offset and end at the last element of the array. If length is provided and is positive, it will end at offset + length position from the beginning of the array.&amp;nbsp;Conversely, if length is provided and is negative, it will end at count(input_array) – length position from the end of the array.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Consider an example:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Alabama&quot;, &quot;Alaska&quot;, &quot;Arizona&quot;, &quot;Arkansas&quot;,&lt;br /&gt;
&quot;California&quot;, &quot;Colorado&quot;, &quot;Connecticut&quot;);&lt;br /&gt;
$subset = array_slice($states, 4);&lt;br /&gt;
print_r($subset);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; California [1] =&amp;gt; Colorado [2] =&amp;gt; Connecticut )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Consider a second example, this one involving a negative length:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Alabama&quot;, &quot;Alaska&quot;, &quot;Arizona&quot;, &quot;Arkansas&quot;,&lt;br /&gt;
&quot;California&quot;, &quot;Colorado&quot;, &quot;Connecticut&quot;);&lt;br /&gt;
$subset = array_slice($states, 2, -2);&lt;br /&gt;
print_r($subset);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Arizona [1] =&amp;gt; Arkansas [2] =&amp;gt; California )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Setting the optional preserve_keys parameter (introduced in PHP 5.0.2) to true will cause the array values’ keys to be preserved in the returned array.&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/sorting-arrays.html&quot;&gt;Sorting Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/splicing-and-dissecting-arrays.html&quot;&gt;Splicing and Dissecting Arrays&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/9028665449909700392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/merging-and-slicing-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/9028665449909700392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/9028665449909700392'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/merging-and-slicing-arrays.html' title='Merging And Slicing Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-1514211056384447344</id><published>2013-07-11T21:32:00.000+05:30</published><updated>2013-07-16T21:32:45.303+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="krsort()"/><category scheme="http://www.blogger.com/atom/ns#" term="ksort()"/><category scheme="http://www.blogger.com/atom/ns#" term="natcasesort()"/><category scheme="http://www.blogger.com/atom/ns#" term="natsort()"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="usort()"/><title type='text'>Sorting Arrays :: continued</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array Naturally&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The natsort() function is intended to offer a sorting mechanism comparable to the mechanisms that people normally use. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void natsort(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The PHP manual offers an excellent example, shown here, of what it means to sort an array “naturally.” Consider the following items:&amp;nbsp;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;picture1.jpg, picture2.jpg, picture10.jpg, picture20.jpg.&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Sorting these items using typical algorithms results in the following ordering:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
picture1.jpg, picture10.jpg, picture2.jpg, picture20.jpg&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Certainly not what you might have expected, right? The natsort() function resolves this dilemma, sorting the array in the order you would expect, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
picture1.jpg, picture2.jpg, picture10.jpg, picture20.jpg&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Case-Insensitive Natural Sorting&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function natcasesort() is functionally identical to natsort(), except that it is case insensitive:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void natcasesort(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Returning to the file-sorting dilemma raised in the natsort() section, suppose that the pictures are named like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&amp;nbsp; &lt;br /&gt;Picture1.JPG, picture2.jpg, PICTURE10.jpg, picture20.jpg &lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The natsort() function would do its best, sorting these items like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
PICTURE10.jpg, Picture1.JPG, picture2.jpg, picture20.jpg&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The natcasesort() function resolves this idiosyncrasy, sorting as you might expect:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Picture1.jpg, PICTURE10.jpg, picture2.jpg, picture20.jpg&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array by Key Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The ksort() function sorts an array by its keys, returning TRUE on success and FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;integer ksort(array array [, int sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional sort_flags parameter is included, the exact sorting behavior is determined by its value, as described in the sort() section. Keep in mind that the behavior will be applied to key sorting but not to value sorting.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting Array Keys in Reverse Order&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The krsort() function operates identically to ksort(), sorting by key, except that it sorts in reverse (descending) order. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;integer krsort(array array [, int sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting According to User-Defined Criteria&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The usort() function offers a means for sorting an array by using a user-defined comparison algorithm, embodied within a function. This is useful when you need to sort data in a fashion not offered by one of PHP’s built-in sorting functions. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void usort(array array, callback function_name)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The user-defined function must take as input two arguments and must return a negative integer, zero, or a positive integer, respectively, based on whether the first argument is less than, equal to, or greater than the second argument. Not surprisingly, this function must be made available to the same scope in which usort() is being called.&lt;br /&gt;A particularly applicable example of where usort() comes in handy involves the ordering of American-format dates (month, day, year, as opposed to day, month, year used by most other countries). Suppose that you want to sort an array of dates in ascending order. While you might think the sort() or natsort() functions are suitable for the job, as it turns out, both produce undesirable results.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;The only recourse is to create a custom function capable of sorting these dates in the correct ordering:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;$dates = array(&#39;10-10-2011&#39;, &#39;2-17-2010&#39;, &#39;2-16-2011&#39;,&lt;br /&gt;&#39;1-01-2013&#39;, &#39;10-10-2012&#39;);&lt;br /&gt;sort($dates);&lt;br /&gt;echo &quot;&amp;lt;p&amp;gt;Sorting the array using the sort() function:&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;print_r($dates);&lt;br /&gt;natsort($dates);&lt;br /&gt;echo &quot;&amp;lt;p&amp;gt;Sorting the array using the natsort() function: &amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;print_r($dates);&lt;br /&gt;function DateSort($a, $b) {&lt;br /&gt;// If the dates are equal, do nothing.&lt;br /&gt;if($a == $b) return 0;&lt;br /&gt;// Disassemble dates&lt;br /&gt;list($amonth, $aday, $ayear) = explode(&#39;-&#39;,$a);&lt;br /&gt;list($bmonth, $bday, $byear) = explode(&#39;-&#39;,$b);&lt;br /&gt;// Pad the month with a leading zero if leading number not present&lt;br /&gt;$amonth = str_pad($amonth, 2, &quot;0&quot;, STR_PAD_LEFT);&lt;br /&gt;$bmonth = str_pad($bmonth, 2, &quot;0&quot;, STR_PAD_LEFT);&lt;br /&gt;// Pad the day with a leading zero if leading number not present&lt;br /&gt;$aday = str_pad($aday, 2, &quot;0&quot;, STR_PAD_LEFT);&lt;br /&gt;$bday = str_pad($bday, 2, &quot;0&quot;, STR_PAD_LEFT);&lt;br /&gt;// Reassemble dates&lt;br /&gt;$a = $ayear . $amonth . $aday;&lt;br /&gt;$b = $byear . $bmonth . $bday;&lt;br /&gt;// Determine whether date $a &amp;gt; $date b&lt;br /&gt;return ($a &amp;gt; $b) ? 1 : -1;&lt;br /&gt;}&lt;br /&gt;usort($dates, &#39;DateSort&#39;);&lt;br /&gt;echo &quot;&amp;lt;p&amp;gt;Sorting the array using the user-defined DateSort() function: &amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;print_r($dates);&lt;br /&gt;?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following (formatted for readability):&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Sorting the array using the sort() function:&lt;br /&gt;Array ( [0] =&amp;gt; 1-01-2013 [1] =&amp;gt; 10-10-2011 [2] =&amp;gt; 10-10-2012&lt;br /&gt;[3] =&amp;gt; 2-16-2011 [4] =&amp;gt; 2-17-2010 ) &lt;br /&gt;&lt;br /&gt;Sorting the array using the natsort() function:&lt;br /&gt;Array ( [0] =&amp;gt; 1-01-2013 [3] =&amp;gt; 2-16-2011 [4] =&amp;gt; 2-17-2010&lt;br /&gt;[1] =&amp;gt; 10-10-2011 [2] =&amp;gt; 10-10-2012 ) &lt;br /&gt;&lt;br /&gt;Sorting the array using the user-defined DateSort() function:&lt;br /&gt;Array ( [0] =&amp;gt; 2-17-2010 [1] =&amp;gt; 2-16-2011 [2] =&amp;gt; 10-10-2011&lt;br /&gt;[3] =&amp;gt; 10-10-2012 [4] =&amp;gt; 1-01-2013 )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/sorting-arrays.html&quot;&gt;Sorting Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/merging-and-slicing-arrays.html&quot;&gt;Merging and Slicing Arrays&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/1514211056384447344/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/sorting-arrays-continued.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1514211056384447344'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1514211056384447344'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/sorting-arrays-continued.html' title='Sorting Arrays :: continued'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-3086037686864585036</id><published>2013-07-10T21:26:00.000+05:30</published><updated>2013-07-16T21:45:20.065+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array_flip"/><category scheme="http://www.blogger.com/atom/ns#" term="array_reverse"/><category scheme="http://www.blogger.com/atom/ns#" term="arsort"/><category scheme="http://www.blogger.com/atom/ns#" term="asort"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="rsort"/><category scheme="http://www.blogger.com/atom/ns#" term="sort"/><category scheme="http://www.blogger.com/atom/ns#" term="sorting arrays"/><title type='text'>Sorting Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
To be sure, data sorting is a central topic of computer science. Anybody who’s taken an entry-level programming class is well aware of sorting algorithms such as bubble, heap, shell, and quick. This subject rears its head so often during daily programming tasks that the process of sorting data is as common as creating an &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-8-control-structures.html&quot;&gt;if conditional or a while loop&lt;/a&gt;&lt;/strong&gt;. PHP facilitates the process by offering a multitude of useful &lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;&lt;b&gt;functions&lt;/b&gt;&lt;/a&gt; capable of sorting arrays (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;&lt;b&gt;what is an array&lt;/b&gt;&lt;/a&gt;)&amp;nbsp; in a variety of manners.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; By default, PHP’s sorting functions sort in accordance with the rules as specified by the English language. If&amp;nbsp;you need to sort in another language, say French or German, you’ll need to modify this default behavior by setting&amp;nbsp;your locale using the&lt;strong&gt; setlocale()&lt;/strong&gt; function.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Reversing Array Element Order&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_reverse() function reverses an array’s element order. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_reverse(array array [, boolean preserve_keys])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional preserve_keys parameter is set to TRUE, the key mappings are maintained. Otherwise, each newly rearranged value will assume the key of the value previously presiding at that position:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Delaware&quot;, &quot;Pennsylvania&quot;, &quot;New Jersey&quot;);&lt;br /&gt;
print_r(array_reverse($states));&lt;br /&gt;
// Array ( [0] =&amp;gt; New Jersey [1] =&amp;gt; Pennsylvania [2] =&amp;gt; Delaware )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Contrast this behavior with that resulting from enabling preserve_keys:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Delaware&quot;, &quot;Pennsylvania&quot;, &quot;New Jersey&quot;);&lt;br /&gt;
print_r(array_reverse($states,1));&lt;br /&gt;
// Array ( [2] =&amp;gt; New Jersey [1] =&amp;gt; Pennsylvania [0] =&amp;gt; Delaware )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Arrays with associative keys are not affected by preserve_keys; key mappings are always preserved in this case.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Flipping Array Keys and Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_flip() function reverses the roles of the keys and their corresponding values in an array. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_flip(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state = array(&quot;Delaware&quot;, &quot;Pennsylvania&quot;, &quot;New Jersey&quot;);&lt;br /&gt;
$state = array_flip($state);&lt;br /&gt;
print_r($state);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This example returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [Delaware] =&amp;gt; 0 [Pennsylvania] =&amp;gt; 1 [New Jersey] =&amp;gt; 2 )&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The sort() function sorts an array, ordering elements from lowest to highest value. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void sort(array array [, int sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The sort() function doesn’t return the sorted array. Instead, it sorts the array “in place,” returning nothing, regardless of outcome. The optional sort_flags parameter modifies the function’s default behavior in accordance with its assigned value:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;SORT_NUMERIC:&lt;/strong&gt; Sorts items numerically. This is useful when sorting integers or floats.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;SORT_REGULAR:&lt;/strong&gt; Sorts items by their ASCII value. This means that B will come before a, for instance.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;SORT_STRING:&lt;/strong&gt; Sorts items in a fashion that better corresponds with how a human might perceive the correct order. See &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/sorting-arrays-continued.html&quot;&gt;&lt;strong&gt;natsort()&lt;/strong&gt; &lt;/a&gt;for more information about this matter, introduced later in this section.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align=&quot;justify&quot;&gt;
Consider an example. Suppose you want to sort exam grades from lowest to highest:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$grades = array(42, 98, 100, 100, 43, 12);&lt;br /&gt;
sort($grades);&lt;br /&gt;
print_r($grades);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The outcome looks like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; 12 [1] =&amp;gt; 42 [2] =&amp;gt; 43 [3] =&amp;gt; 98 [4] =&amp;gt; 100 [5] =&amp;gt; 100 )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
It’s important to note that key/value associations are not maintained. Consider the following example:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;OH&quot; =&amp;gt; &quot;Ohio&quot;, &quot;CA&quot; =&amp;gt; &quot;California&quot;, &quot;MD&quot; =&amp;gt; &quot;Maryland&quot;);&lt;br /&gt;
sort($states);&lt;br /&gt;
print_r($states);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Here’s the output:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; California [1] =&amp;gt; Maryland [2] =&amp;gt; Ohio )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
To maintain these associations, use asort().&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array While Maintaining Key/Value Pairs&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The asort() function is identical to sort(), sorting an array in ascending order, except that the key/value correspondence is maintained. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void asort(array array [, integer sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Consider an array that contains the states in the order in which they joined the Union:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[0] = &quot;Delaware&quot;;&lt;br /&gt;
$state[1] = &quot;Pennsylvania&quot;;&lt;br /&gt;
$state[2] = &quot;New Jersey&quot;;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Sorting this array using sort() produces the following ordering (note that the associative correlation are lost, which is probably a bad idea):&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Delaware [1] =&amp;gt; New Jersey [2] =&amp;gt; Pennsylvania )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
However, sorting with asort() produces the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Delaware [2] =&amp;gt; New Jersey [1] =&amp;gt; Pennsylvania )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If you use the optional sort_flags parameter, the exact sorting behavior is determined by its value, as described in the sort() section.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array in Reverse Order&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The rsort() function is identical to sort(), except that it sorts array items in reverse (descending) order. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void rsort(array array [, int sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;Florida&quot;, &quot;Massachusetts&quot;, &quot;Montana&quot;);&lt;br /&gt;
rsort($states);&lt;br /&gt;
print_r($states);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
It returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Ohio [1] =&amp;gt; Montana [2] =&amp;gt; Massachusetts [3] =&amp;gt; Florida )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional sort_flags parameter is included, the exact sorting behavior is determined by its value, as explained in the sort() section.&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Sorting an Array in Reverse Order While Maintaining Key/Value Pairs&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Like asort(), arsort() maintains key/value correlation. However, it sorts the array in reverse order. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void arsort(array array [, int sort_flags])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Delaware&quot;, &quot;Pennsylvania&quot;, &quot;New Jersey&quot;);&lt;br /&gt;
arsort($states);&lt;br /&gt;
print_r($states);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
It returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [1] =&amp;gt; Pennsylvania [2] =&amp;gt; New Jersey [0] =&amp;gt; Delaware )&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional sort_flags parameter is included, the exact sorting behavior is determined by its value, as described in the sort() section.&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/array-size-and-uniqueness.html&quot;&gt;Determining Array Size and Uniqueness&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/sorting-arrays-continued.html&quot;&gt;Sorting Arrays :: continued&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/3086037686864585036/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/sorting-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/3086037686864585036'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/3086037686864585036'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/sorting-arrays.html' title='Sorting Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-5541970602528652926</id><published>2013-07-09T21:14:00.000+05:30</published><updated>2013-07-16T21:17:08.587+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array size and uniqueness"/><category scheme="http://www.blogger.com/atom/ns#" term="array_count_values"/><category scheme="http://www.blogger.com/atom/ns#" term="array_unique"/><category scheme="http://www.blogger.com/atom/ns#" term="count()"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Determining Array Size and Uniqueness</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
A few functions&amp;nbsp;(&lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;more about functions in PHP&lt;/a&gt;&lt;/b&gt;) are available in PHP for determining the number of total and unique array (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;&lt;b&gt;what is an array&lt;/b&gt;&lt;/a&gt;) values. These functions are introduced in this section.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Determining the Size of an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The count() function returns the total number of values found in an array. Its prototype is as follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;integer count(array array [, int mode])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional mode parameter is enabled ( or you can say- set to 1), the array will be counted recursively, a feature useful when counting all the elements of a multidimensional array. The first example counts the total number of vegetables found in the $garden array:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$garden = array(&quot;cabbage&quot;, &quot;peppers&quot;, &quot;turnips&quot;, &quot;carrots&quot;);&lt;br /&gt;echo count($garden);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
4&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The next example counts both the &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;scalar values&lt;/a&gt; &lt;/strong&gt;and&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt; array values&lt;/a&gt;&lt;/strong&gt; found in $locations&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot;&gt; &lt;strong&gt;identifier&lt;/strong&gt;&lt;/a&gt;:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$locations = array(&quot;Italy&quot;, &quot;Amsterdam&quot;, array(&quot;Boston&quot;,&quot;Des Moines&quot;), &quot;Miami&quot;);&lt;br /&gt;echo count($locations, 1);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
6&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
You may be scratching your head at this outcome because there appears to be only five elements in the array. It’s because the array entity holding Boston and Des Moines is counted as an item, just as its contents are.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The sizeof() function is an alias of count(). It is functionally identical.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Counting Array Value Frequency&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_count_values() function returns an array consisting of associative key/value pairs. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;array array_count_values(array array)&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Each key represents a value found in the input_array, and its corresponding value denotes the frequency of that key’s appearance (as a value) in the input_array. An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;Iowa&quot;, &quot;Arizona&quot;, &quot;Iowa&quot;, &quot;Ohio&quot;);&lt;br /&gt;$stateFrequency = array_count_values($states);&lt;br /&gt;print_r($stateFrequency);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [Ohio] =&amp;gt; 2 [Iowa] =&amp;gt; 2 [Arizona] =&amp;gt; 1 )&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Determining Unique Array Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_unique() function removes all duplicate values found in an array, returning an array&amp;nbsp;(&lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot;&gt;more about arrays in PHP&lt;/a&gt;&lt;/b&gt;) consisting of solely unique values. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;array array_unique(array array [, int sort_flags = SORT_STRING])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;Iowa&quot;, &quot;Arizona&quot;, &quot;Iowa&quot;, &quot;Ohio&quot;);&lt;br /&gt;$uniqueStates = array_unique($states);&lt;br /&gt;print_r($uniqueStates);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Array ( [0] =&amp;gt; Ohio [1] =&amp;gt; Iowa [2] =&amp;gt; Arizona )&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The optional sort_flags parameter (added in PHP 5.2.9) determines how the array values are sorted. By default, they will be sorted as strings &lt;strong&gt;(&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;what is string data type&lt;/a&gt;)&lt;/strong&gt;; however, you also have the option of sorting them numerically (SORT_NUMERIC), using PHP’s default sorting methodology (SORT_REGULAR), or according to a locale (SORT_LOCALE_STRING).&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/traversing-arrays.html&quot;&gt;Traversing Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/sorting-arrays.html&quot;&gt;Sorting Arrays&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/5541970602528652926/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/array-size-and-uniqueness.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5541970602528652926'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5541970602528652926'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/array-size-and-uniqueness.html' title='Determining Array Size and Uniqueness'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-7260779377685062227</id><published>2013-07-08T21:01:00.000+05:30</published><updated>2013-07-16T21:47:54.536+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array_walk()"/><category scheme="http://www.blogger.com/atom/ns#" term="current()"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="each()"/><category scheme="http://www.blogger.com/atom/ns#" term="end()"/><category scheme="http://www.blogger.com/atom/ns#" term="key()"/><category scheme="http://www.blogger.com/atom/ns#" term="next()"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="prev()"/><category scheme="http://www.blogger.com/atom/ns#" term="reset()"/><category scheme="http://www.blogger.com/atom/ns#" term="traversing arrays"/><title type='text'>Traversing Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The need to travel across an array&lt;b&gt;&amp;nbsp;(&lt;/b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot; style=&quot;font-weight: bold;&quot;&gt;what is an array&lt;/a&gt;&lt;b&gt;)&lt;/b&gt; and retrieve various keys, values, or both is common, so it’s not a surprise that PHP offers numerous functions suited to this need. Many of these functions do double duty: &lt;em&gt;retrieving the key or value residing at the current pointer location, and moving the pointer to the next appropriate location.&lt;/em&gt; These functions are introduced here.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Retrieving the Current Array Key&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The key() functions (&lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;more about functions in PHP&lt;/a&gt;&lt;/b&gt;) returns the key located at the current pointer position of the provided array. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed key(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The following example will output the $capitals array keys by iterating over the array and moving the pointer:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$capitals = array(&quot;Ohio&quot; =&amp;gt; &quot;Columbus&quot;, &quot;Iowa&quot; =&amp;gt; &quot;Des Moines&quot;);&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Can you name the capitals of these states?&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
while($key = key($capitals)) {&lt;br /&gt;
printf(&quot;%s &amp;lt;br /&amp;gt;&quot;, $key);&lt;br /&gt;
next($capitals);&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Can you name the capitals of these states?&lt;br /&gt;
Ohio&lt;br /&gt;
Iowa&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; key() does not advance the pointer with each call. Rather, you use the next() function, whose sole purpose is to accomplish this task. This function is introduced later in this section.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Retrieving the Current Array Value&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The current() function returns the array value residing at the current pointer position of the array. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed current(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Let’s revise the previous example, this time retrieving the array values:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$capitals = array(&quot;Ohio&quot; =&amp;gt; &quot;Columbus&quot;, &quot;Iowa&quot; =&amp;gt; &quot;Des Moines&quot;);&lt;br /&gt;
echo &quot;&amp;lt;p&amp;gt;Can you name the states belonging to these capitals?&amp;lt;/p&amp;gt;&quot;;&lt;br /&gt;
while($capital = current($capitals)) {&lt;br /&gt;
printf(&quot;%s &amp;lt;br /&amp;gt;&quot;, $capital);&lt;br /&gt;
next($capitals);&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The output follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Can you name the states belonging to these capitals?&lt;br /&gt;
Columbus&lt;br /&gt;
Des Moines&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Retrieving the Current Array Key and Value&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The each() function returns the current key/value pair from the array and advances the pointer one position. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array each(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The returned array consists of four keys, with keys 0 and key containing the key name, and keys 1 and value containing the corresponding data. If the pointer is residing at the end of the array before executing each(), FALSE is returned.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;u&gt;Moving the Array Pointer&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Several functions are available for moving the array pointer. These functions are introduced in this section.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Moving the Pointer to the Next Array Position&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The next() function returns the array value residing at the position immediately following that of the current array pointer. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed next(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
An example follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$fruits = array(&quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;);&lt;br /&gt;
$fruit = next($fruits); // returns &quot;orange&quot;&lt;br /&gt;
$fruit = next($fruits); // returns &quot;banana&quot;&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Moving the Pointer to the Previous Array Position&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The prev() function returns the array value residing at the location preceding the current pointer location, or FALSE if the pointer resides at the first position in the array. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed prev(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Because prev() works in exactly the same fashion as next(), no example is necessary.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Moving the Pointer to the First Array Position&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
The reset() function serves to set an array pointer back to the beginning of the array. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed reset(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This function is commonly used when you need to review or manipulate an array multiple times within a script, or when sorting has completed.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Moving the Pointer to the Last Array Position&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The end() function moves the pointer to the last position of an array, returning the last element. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;&lt;strong&gt;mixed end(array array)&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example demonstrates retrieving the first and last array values:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$fruits = array(&quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;);&lt;br /&gt;
$fruit = current($fruits); // returns &quot;apple&quot;&lt;br /&gt;
$fruit = end($fruits); // returns &quot;banana&quot;&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Passing Array Values to a Function&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_walk() function will pass each element of an array to the user-defined function. This is useful when you need to perform a particular action based on each array element. If you intend to actually modify the array key/value pairs, you’ll need to pass each key/value to the function as a reference. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial;&quot;&gt;boolean array_walk(array &amp;amp;array, callback function [, mixed userdata])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The user-defined function must take two parameters as input. The first represents the array’s current value, and the second represents the current key. If the optional &lt;em&gt;userdata&lt;/em&gt; parameter is present in the call to array_walk(), its value will be passed as a third parameter to the user-defined function.&lt;br /&gt;
You are probably scratching your head, wondering how this function could possibly be of any use. Perhaps one of the most effective examples involves the sanity-checking of user-supplied form data. Suppose the user is asked to provide six keywords that he thinks best describe the state in which he lives. A sample form is provided in Listing 5-1.&lt;/div&gt;
&lt;br /&gt;
&lt;em&gt;&lt;u&gt;Listing 5-1. Using an Array in a Form&lt;/u&gt;&lt;/em&gt; &lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;form action=&quot;submitdata.php&quot; method=&quot;post&quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Provide up to six keywords that you believe best describe the state in&lt;br /&gt;
which you live:&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 1:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 2:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 3:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 4:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 5:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Keyword 6:&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&quot;text&quot; name=&quot;keyword[]&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot; /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;input type=&quot;submit&quot; value=&quot;Submit!&quot;&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This form information is then sent to some script, referred to as &lt;em&gt;submitdata.php&lt;/em&gt; in the form. This script should sanitize user data and then insert it into a database for later review. Using array_walk(), you can easily filter the keywords using a predefined function:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br /&gt;
function sanitize_data(&amp;amp;amp;$value, $key) {&lt;br /&gt;
$value = strip_tags($value);&lt;br /&gt;
}&lt;br /&gt;
array_walk($_POST[&#39;keyword&#39;],&quot;sanitize_data&quot;);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The result is that each value in the array is run through the strip_tags() function, which results in any HTML and PHP tags being deleted from the value. Of course, additional input checking would be necessary, but this should suffice to illustrate the utility of array_walk().&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If you’re working with arrays of arrays, the array_walk_recursive() function (introduced in PHP 5.0) is capable to recursively apply a user-defined function to every element in an array.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/locating-array-elements.html&quot;&gt;Locating Array Elements&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/array-size-and-uniqueness.html&quot;&gt;Determining Array Size and Uniqueness&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/7260779377685062227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/traversing-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/7260779377685062227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/7260779377685062227'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/traversing-arrays.html' title='Traversing Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-2080196816285412836</id><published>2013-07-06T00:41:00.000+05:30</published><updated>2013-07-09T00:49:16.911+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array_key_exists"/><category scheme="http://www.blogger.com/atom/ns#" term="array_keys"/><category scheme="http://www.blogger.com/atom/ns#" term="array_search"/><category scheme="http://www.blogger.com/atom/ns#" term="array_values"/><category scheme="http://www.blogger.com/atom/ns#" term="in_array"/><category scheme="http://www.blogger.com/atom/ns#" term="locating array elements"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Locating array elements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The ability to efficiently sift through data is absolutely crucial in today’s information-driven society. This article introduces several functions that enable you to search arrays(&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;&lt;strong&gt;what is an array&lt;/strong&gt;&lt;/a&gt;?) in order to locate items of interest.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Searching an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The in_array() function searches an array for a specific value, returning TRUE if the value is found and FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;boolean in_array(mixed needle, array haystack [, boolean strict])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; If the needle is a string, then, comparison is&amp;nbsp; case sensitive.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
In the following example, a message is output if a specified state (Ohio) is found in an array consisting of states having statewide smoking bans:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state = &quot;Ohio&quot;;&lt;br /&gt;$states = array(&quot;California&quot;, &quot;Hawaii&quot;, &quot;Ohio&quot;, &quot;New York&quot;);&lt;br /&gt;if(in_array($state, $states)) echo &quot;Not to worry, $state is smoke-free!&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The optional third parameter, strict, forces in_array() to also consider type.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;$a = array(&#39;1.10&#39;, 12.4, 1.13);&lt;br /&gt;if (in_array(&#39;12.4&#39;, $a, true)) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;&#39;12.4&#39; found with strict check\n&quot;;&lt;br /&gt;}&lt;br /&gt;if (in_array(1.13, $a, true)) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;1.13 found with strict check\n&quot;;&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
The above example will output:&lt;br /&gt;&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
1.13 found with strict check&lt;br /&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Searching Associative Array Keys&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The function array_key_exists() returns TRUE if a specified key is found in an array and FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;boolean array_key_exists(mixed key, array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example will search an array’s keys for Ohio, and if found, will output information about its entrance into the Union:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[&quot;Delaware&quot;] = &quot;December 7, 1787&quot;;&lt;br /&gt;$state[&quot;Pennsylvania&quot;] = &quot;December 12, 1787&quot;;&lt;br /&gt;$state[&quot;Ohio&quot;] = &quot;March 1, 1803&quot;;&lt;br /&gt;if (array_key_exists(&quot;Ohio&quot;, $state))&lt;br /&gt;printf(&quot;Ohio joined the Union on %s&quot;, $state[&quot;Ohio&quot;]);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The following is the result:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Ohio joined the Union on March 1, 1803&lt;br /&gt;
&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Searching Associative Array Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_search() function searches an array for a specified value, returning its key if located and FALSE otherwise. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed array_search(mixed needle, array haystack [, boolean strict])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example searches $state for a particular date (December 7), returning information about the corresponding state if located:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[&quot;Ohio&quot;] = &quot;March 1&quot;;&lt;br /&gt;$state[&quot;Delaware&quot;] = &quot;December 7&quot;;&lt;br /&gt;$state[&quot;Pennsylvania&quot;] = &quot;December 12&quot;;&lt;br /&gt;$founded = array_search(&quot;December 7&quot;, $state);&lt;br /&gt;if ($founded) printf(&quot;%s was founded on %s.&quot;, $founded, $state[$founded]);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The output follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Delaware was founded on December 7.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Retrieving Array Keys&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_keys() function returns an array consisting of all keys located in an array. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_keys(array array [, mixed search_value [, boolean preserve_keys]])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If the optional search_value parameter is included, only keys matching that value will be returned.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example outputs all of the key values found in the $state array:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[&quot;Delaware&quot;] = &quot;December 7, 1787&quot;;&lt;br /&gt;$state[&quot;Pennsylvania&quot;] = &quot;December 12, 1787&quot;;&lt;br /&gt;$state[&quot;New Jersey&quot;] = &quot;December 18, 1787&quot;;&lt;br /&gt;$keys = array_keys($state);&lt;br /&gt;print_r($keys);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The output follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; Delaware [1] =&amp;gt; Pennsylvania [2] =&amp;gt; New Jersey )&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Setting the optional preserve_keys parameter (introduced in PHP 5.0.2) to true will cause the array values’ keys to be preserved in the returned array.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Retrieving Array Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_values() &lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;&lt;strong&gt;functions&lt;/strong&gt;&lt;/a&gt; returns all values located in an array, automatically providing numeric indexes for the returned array. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array_values(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example will retrieve the population numbers for all of the states found in&amp;nbsp;&lt;span style=&quot;text-align: left;&quot;&gt;$population:&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$population = array(&quot;Ohio&quot; =&amp;gt; &quot;11,421,267&quot;, &quot;Iowa&quot; =&amp;gt; &quot;2,936,760&quot;);&lt;br /&gt;print_r(array_values($population));&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
This example will output the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; 11,421,267 [1] =&amp;gt; 2,936,760 )&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Now take another example:-&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;$array = array(&quot;size&quot; =&amp;gt; &quot;XL&quot;, &quot;color&quot; =&amp;gt; &quot;gold&quot;,&quot;x&quot; =&amp;gt; &quot; &quot;,&quot;y&quot; =&amp;gt; &quot;gold&quot;,&quot;z&quot; =&amp;gt; &quot;&quot;);&lt;br /&gt;print_r(array_values($array));&lt;br /&gt;?&amp;gt; &lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Above example will produce the following result:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [0] =&amp;gt; XL [1] =&amp;gt; gold [2] =&amp;gt; [3] =&amp;gt; gold [4] =&amp;gt; )&lt;br /&gt;&lt;/div&gt;
&amp;nbsp;&lt;br /&gt;&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/adding-and-removing-array-elements.html&quot;&gt;Adding and Removing array Elements&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/traversing-arrays.html&quot;&gt;Traversing Arrays&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/2080196816285412836/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/locating-array-elements.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/2080196816285412836'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/2080196816285412836'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/locating-array-elements.html' title='Locating array elements'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-6475316814881588120</id><published>2013-07-05T00:40:00.000+05:30</published><updated>2013-07-09T00:27:59.627+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="adding array elements"/><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="pop"/><category scheme="http://www.blogger.com/atom/ns#" term="push"/><category scheme="http://www.blogger.com/atom/ns#" term="removing array elements"/><category scheme="http://www.blogger.com/atom/ns#" term="shift"/><category scheme="http://www.blogger.com/atom/ns#" term="unshift"/><title type='text'>Adding and Removing Array Elements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP provides a number of &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;functions&lt;/a&gt;&lt;/strong&gt; for both growing and shrinking an array (&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot;&gt;what is an array?&lt;/a&gt;)&lt;/strong&gt;. Some of these functions are provided as a convenience to programmers who wish to mimic various queue implementations (&lt;em&gt;FIFO, LIFO&lt;/em&gt;, etc.), as reflected by their names (push, pop, shift, and unshift). In this article we’ll be covering these topics and many more with examples.&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; A traditional queue is a data structure in which the elements are removed in the same order in which they were entered, known as first-in-first-out or FIFO. In contrast, a stack is a data structure in which the elements are removed in the order opposite to that in which they were entered, known as last-in-first-out or LIFO.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Adding a Value to the Front of an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_unshift() function adds elements to the front of the array. Returns the new number of elements in the array.&amp;nbsp; All preexisting numerical keys are modified to reflect their new position in the array, but associative keys aren’t affected. Its prototype follows:&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;int array_unshift(array array, mixed variable [, mixed variable...])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example adds two states to the front of the $states array:&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;New York&quot;);&lt;br /&gt;
array_unshift($states, &quot;California&quot;, &quot;Texas&quot;);&lt;br /&gt;
// $states = array(&quot;California&quot;, &quot;Texas&quot;, &quot;Ohio&quot;, &quot;New York&quot;);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Adding a Value to the End of an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_push() function adds a value to the end of an array, returning the total count of elements in the array after the new value has been added. You can push multiple variables onto the array simultaneously by passing these variables into the function as input parameters. Its prototype follows:&lt;/div&gt;
&lt;br /&gt;&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;int array_push(array array, mixed variable [, mixed variable...])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; If you use array_push() to add one element to the array it&#39;s better to use $array[] = because in that way there is no overhead of calling a function.&lt;br /&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If you&#39;re going to use array_push() to insert a &quot;$key&quot; =&amp;gt; &quot;$value&quot; pair into an array, it can be done using the following:&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $data[$key] = $value;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;&lt;br /&gt;
It is not necessary to use array_push.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;&lt;br /&gt;
The following example adds two more states onto the $states array:&lt;/div&gt;
&lt;br /&gt;&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;New York&quot;);&lt;br /&gt;
array_push($states, &quot;California&quot;, &quot;Texas&quot;);&lt;br /&gt;
// $states = array(&quot;Ohio&quot;, &quot;New York&quot;, &quot;California&quot;, &quot;Texas&quot;);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Removing a Value from the Front of an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_shift() function removes and returns the first item found in an array, or NULL if array is empty or is not an array.&amp;nbsp; If numerical keys are used, all corresponding values will be shifted down, whereas arrays using associative keys will not be affected. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed array_shift(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example removes the first state from the $states array:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;New York&quot;, &quot;California&quot;, &quot;Texas&quot;);&lt;br /&gt;
$state = array_shift($states);&lt;br /&gt;
// $states = array(&quot;New York&quot;, &quot;California&quot;, &quot;Texas&quot;)&lt;br /&gt;
// $state = &quot;Ohio&quot;&lt;/div&gt;&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; If you want a version of array_shift() that works non-destructively (i.e., an easy function to grab the first element of the array without modifying the array), try reset(), which returns the value of first element or False if array is empty.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Removing a Value from the End of an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array_pop() function removes and returns the last element from an array. If array is empty (or is not an array), NULL will be returned.&amp;nbsp; Its prototype follows:&lt;/div&gt;
&lt;br /&gt;&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;mixed array_pop(array array)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The following example removes the last state from the $states array:&lt;/div&gt;
&lt;br /&gt;&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;New York&quot;, &quot;California&quot;, &quot;Texas&quot;);&lt;br /&gt;
$state = array_pop($states);&lt;br /&gt;
// $states = array(&quot;Ohio&quot;, &quot;New York&quot;, &quot;California&quot;&lt;br /&gt;
// $state = &quot;Texas&quot;&lt;/div&gt;
&lt;br /&gt;&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; If you want a version of array_pop() that works non-destructively (i.e., an easy function to grab the last element of the array without modifying the array), try end(), which returns the value of last element or False if array is empty.&lt;/div&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
You can see a single task can be accomplished by a number of ways. It depends on your comfort and demand of the situation- which method to choose.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/outputting-arrays.html&quot;&gt;Outputting arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/locating-array-elements.html&quot;&gt;Locating Array elements&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/6475316814881588120/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/adding-and-removing-array-elements.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6475316814881588120'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6475316814881588120'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/adding-and-removing-array-elements.html' title='Adding and Removing Array Elements'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-4005170346394683721</id><published>2013-07-03T00:39:00.000+05:30</published><updated>2013-07-05T01:39:02.248+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="outputting arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="print_r"/><category scheme="http://www.blogger.com/atom/ns#" term="vprintf"/><category scheme="http://www.blogger.com/atom/ns#" term="vsprintf"/><title type='text'>Outputting Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Now that you know &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/arrays.html&quot;&gt;what is an array&lt;/a&gt;&lt;/strong&gt;? and &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/creating-arrays.html&quot;&gt;how to create an array&lt;/a&gt;&lt;/strong&gt;?, it is the time to learn how to &lt;strong&gt;&lt;em&gt;output arrays&lt;/em&gt;&lt;/strong&gt;. The most common way to output an array’s contents is by iterating over each key and echoing the corresponding value. For instance, a foreach statement does the trick nicely:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Ohio&quot;, &quot;Florida&quot;, &quot;Texas&quot;);&lt;br /&gt;
foreach ($states AS $state) {&lt;br /&gt;
echo &quot;{$state}&amp;lt;br /&amp;gt;&quot;;&lt;br /&gt;
}&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If you want to print an array of arrays or need to exercise a more exacting format standard over array output, consider using the &lt;strong&gt;&lt;em&gt;vprint() function&lt;/em&gt;&lt;/strong&gt;, which allows you to easily display array contents using the same formatting syntax used by the &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-3-outputting-data.html&quot;&gt;printf() and sprintf()&lt;/a&gt;&lt;/strong&gt; functions introduced earlier. Here’s an example:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$customers = array();&lt;br /&gt;
$customers[] = array(&quot;Jason Gilmore&quot;, &quot;jason@example.com&quot;, &quot;614-999-9999&quot;);&lt;br /&gt;
$customers[] = array(&quot;Jesse James&quot;, &quot;jesse@example.net&quot;, &quot;818-999-9999&quot;);&lt;br /&gt;
$customers[] = array(&quot;Donald Duck&quot;, &quot;donald@example.org&quot;, &quot;212-999-9999&quot;);&lt;br /&gt;
foreach ($customers AS $customer) {&lt;br /&gt;
vprintf(&quot;&amp;lt;p&amp;gt;Name: %s&amp;lt;br /&amp;gt;E-mail: %s&amp;lt;br /&amp;gt;Phone: %s&amp;lt;/p&amp;gt;&quot;, $customer);&lt;br /&gt;
}&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Executing this code produces the following output:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Name: Jason Gilmore&amp;lt;br /&amp;gt;&lt;br /&gt;
E-mail: jason@example.com&lt;br /&gt;
&amp;lt;br /&amp;gt;Phone: 614-999-9999&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Name: Jesse James&amp;lt;br /&amp;gt;&lt;br /&gt;
E-mail: jesse@example.net&amp;lt;br /&amp;gt;&lt;br /&gt;
Phone: 818-999-9999&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Name: Donald Duck&amp;lt;br /&amp;gt;&lt;br /&gt;
E-mail: donald@example.org&amp;lt;br /&amp;gt;&lt;br /&gt;
Phone: 212-999-9999&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
If you’d like to send the formatted results to a string, you can use the &lt;strong&gt;&lt;em&gt;vsprintf() function&lt;/em&gt;&lt;/strong&gt;. It is the same as using &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-3-outputting-data.html&quot;&gt;sprintf() function&lt;/a&gt;&lt;/strong&gt;.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Printing Arrays for Testing Purposes&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array contents in most of the previous examples have been displayed using comments. While this works great for instructional purposes, in the real world you’ll need to know how to easily output their contents to the screen for testing purposes. This is most commonly done with the &lt;strong&gt;&lt;em&gt;print_r() function&lt;/em&gt;&lt;/strong&gt;. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;boolean print_r(mixed variable [, boolean return])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The print_r() function accepts a variable and sends its contents to standard output, returning TRUE on success and FALSE otherwise. This in itself isn’t particularly exciting, until you realize it will organize an array’s contents (as well as an object’s) into a readable format. For example, suppose you want to view the contents of an associative array consisting of states and their corresponding state capitals. You could call print_r() like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
print_r($states);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Array ( [Ohio] =&amp;gt; Columbus [Iowa] =&amp;gt; Des Moines [Arizona] =&amp;gt; Phoenix )&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The optional parameter &lt;em&gt;return&lt;/em&gt; modifies the function’s behavior, causing it to return the output to the caller, rather than sending it to the standard output ( which is the browser window ). Therefore, if you want to return the contents of the preceding $states array, you just set return to TRUE:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$stateCapitals = print_r($states, TRUE);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Keep in mind the print_r() function isn’t the only way to output an array; it just offers a convenient means for doing so. You’re free to output arrays using a looping conditional, such as while or for; in fact, using these sorts of loops is required to implement many application features.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; Always use quotes around a string literal array index ( except inside a double-quoted string, it&#39;s valid to not surround array indexes with quotes so &quot;$foo[bar]&quot; is valid). For example, $foo[&#39;bar&#39;] is correct, while $foo[bar] is not. This does not mean to always quote the key. Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$array = array(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;foo&quot; =&amp;gt; &quot;bar&quot;,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;bar&quot; =&amp;gt; &quot;foo&quot;,&lt;br /&gt;
);&lt;br /&gt;
// as of PHP 5.4&lt;br /&gt;
$array = [&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;foo&quot; =&amp;gt; &quot;bar&quot;,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;bar&quot; =&amp;gt; &quot;foo&quot;,&lt;br /&gt;
];&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/creating-arrays.html&quot;&gt;Creating Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/adding-and-removing-array-elements.html&quot;&gt;Adding and Removing Array Elements&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/4005170346394683721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/outputting-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4005170346394683721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/4005170346394683721'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/outputting-arrays.html' title='Outputting Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-1616202945074981673</id><published>2013-07-02T00:38:00.000+05:30</published><updated>2013-07-05T01:43:30.768+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="array()"/><category scheme="http://www.blogger.com/atom/ns#" term="Creating Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="is_array()"/><category scheme="http://www.blogger.com/atom/ns#" term="list()"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="range()"/><category scheme="http://www.blogger.com/atom/ns#" term="Testing arrays"/><title type='text'>Creating Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Unlike other languages, PHP doesn’t require that you assign a size to an array at creation time. In fact, because it’s a loosely typed language, PHP doesn’t even require that you declare the array before using it, although you’re free to do so. Each approach is introduced here, beginning with the informal variety.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Individual elements of a PHP array are referenced by denoting the element between a pair of square brackets. Because there is no size limitation on the array, you can create the array simply by making reference to it, like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[0] = &quot;Delaware&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You can then display the first element of the array $state, like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
echo $state[0];&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Additional values can be added by mapping each new value to an array index, like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[1] = &quot;Pennsylvania&quot;;&lt;br /&gt;
$state[2] = &quot;New Jersey&quot;;&lt;br /&gt;
...&lt;br /&gt;
$state[49] = &quot;Hawaii&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Interestingly, if you intend for the index value to be numerical and ascending, you can omit the index value at creation time:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[] = &quot;Pennsylvania&quot;;&lt;br /&gt;
$state[] = &quot;New Jersey&quot;;&lt;br /&gt;
...&lt;br /&gt;
$state[] = &quot;Hawaii&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;Creating associative arrays in this fashion is equally trivial except that the key is always required&lt;/em&gt;. The following example creates an array that matches U.S. state names with their date of entry into the Union:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$state[&quot;Delaware&quot;] = &quot;December 7, 1787&quot;;&lt;br /&gt;
$state[&quot;Pennsylvania&quot;] = &quot;December 12, 1787&quot;;&lt;br /&gt;
$state[&quot;New Jersey&quot;] = &quot;December 18, 1787&quot;;&lt;br /&gt;
...&lt;br /&gt;
$state[&quot;Hawaii&quot;] = &quot;August 21, 1959&quot;;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Creating Arrays with array()&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The array() construct takes as its input zero or more items and returns an array consisting of these input elements. Its prototype looks like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array array([item1 [,item2 ... [,itemN]]])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Here is an example of using array() to create an indexed array:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$languages = array(&quot;English&quot;, &quot;Gaelic&quot;, &quot;Spanish&quot;);&lt;br /&gt;
// $languages[0] = &quot;English&quot;, $languages[1] = &quot;Gaelic&quot;, $languages[2] = &quot;Spanish&quot;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You can also use array() to create an associative array, like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$languages = array(&quot;Spain&quot; =&amp;gt; &quot;Spanish&quot;,&lt;br /&gt;
&quot;Ireland&quot; =&amp;gt; &quot;Gaelic&quot;,&lt;br /&gt;
&quot;United States&quot; =&amp;gt; &quot;English&quot;);&lt;br /&gt;
// $languages[&quot;Spain&quot;] = &quot;Spanish&quot;&lt;br /&gt;
// $languages[&quot;Ireland&quot;] = &quot;Gaelic&quot;&lt;br /&gt;
// $languages[&quot;United States&quot;] = &quot;English&quot;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Extracting Arrays with list()&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The list() construct is similar to array(), though it’s used to make simultaneous variable assignments (&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot; target=&quot;_blank&quot;&gt;more about variables&lt;/a&gt;) from values extracted from an array in just one operation. Its prototype looks like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;void list(mixed...)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
This construct can be particularly useful when you’re extracting information from a database or file. For example, suppose you wanted to format and output information read from a text file named &lt;em&gt;users.txt&lt;/em&gt;. Each line of the file contains user information, including name, occupation, and favorite color with each item delimited by a vertical bar. A typical line would look similar to the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Nino Sanzi | professional golfer | green&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Using list(), a simple loop could read each line, assign each piece of data to a variable, and format and display the data as needed. Here’s how you could use list() to make multiple variable assignments simultaneously:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
// Open the users.txt file&lt;br /&gt;
$users = fopen(&quot;users.txt&quot;, &quot;r&quot;); &lt;br /&gt;
// While the EOF hasn&#39;t been reached, get next line&lt;br /&gt;
while ($line = fgets($users, 4096)) &lt;br /&gt;
{&lt;br /&gt;
// use explode() to separate each piece of data.&lt;br /&gt;
list($name, $occupation, $color) = explode(&quot;|&quot;, $line); &lt;br /&gt;
// format and output the data&lt;br /&gt;
printf(&quot;Name: %s &amp;lt;br /&amp;gt;&quot;, $name);&lt;br /&gt;
printf(&quot;Occupation: %s &amp;lt;br /&amp;gt;&quot;, $occupation);&lt;br /&gt;
printf(&quot;Favorite color: %s &amp;lt;br /&amp;gt;&quot;, $color);&lt;br /&gt;
} &lt;br /&gt;
fclose($users); &lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Each line of the users.txt file will be read and the browser output formatted similarly to this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Name: Nino Sanzi&lt;br /&gt;
Occupation: professional golfer&lt;br /&gt;
Favorite Color: green&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
Reviewing the example, list() depends on the function explode() (which returns an array) to split each line into three elements, which explode() does by using the vertical bar ( | ) as the element delimiter. These elements are then assigned to $name, $occupation, and $color.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Populating Arrays with a Predefined Value Range&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The &lt;strong&gt;&lt;em&gt;range()&lt;/em&gt;&lt;/strong&gt; function provides an easy way to quickly create and fill an array consisting of a range of low and high integer values. An array containing all integer values in this range is returned. Its prototype looks like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;array range(int low, int high [, int step])&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
For example, suppose you need an array consisting of all possible face values of a die:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$die = range(1, 6);&lt;br /&gt;
// Same as specifying $die = array(1, 2, 3, 4, 5, 6)&lt;br /&gt;
//now putting step value of 2:&lt;br /&gt;
$even = range(0, 20, 2);&lt;br /&gt;
// $even = array(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
The range() function can also be used for character sequences. For example, suppose you want to create an array consisting of the letters A through F:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$letters = range(&quot;A&quot;, &quot;F&quot;);&lt;br /&gt;
// $letters = array(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Testing for an Array&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
When you incorporate arrays into your application, you’ll sometimes need to know whether a particular variable is an array. A built-in function, is_array(), is available for accomplishing this task. Its prototype follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394; font-family: Arial, Helvetica, sans-serif;&quot;&gt;boolean is_array(mixed variable)&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
This function will return True if the variable is an array, otherwise False.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;Florida&quot;);&lt;br /&gt;
$state = &quot;Ohio&quot;;&lt;br /&gt;
printf(&quot;\$states is an array: %s &amp;lt;br /&amp;gt;&quot;, (is_array($states) ? &quot;TRUE&quot; : &quot;FALSE&quot;));&lt;br /&gt;
printf(&quot;\$state is an array: %s &amp;lt;br /&amp;gt;&quot;, (is_array($state) ? &quot;TRUE&quot; : &quot;FALSE&quot;));&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Executing this example produces the following:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
$states is an array: TRUE&lt;br /&gt;
$state is an array: FALSE&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/arrays.html&quot;&gt;Arrays&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/outputting-arrays.html&quot;&gt;Outputting Arrays&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/1616202945074981673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/creating-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1616202945074981673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/1616202945074981673'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/creating-arrays.html' title='Creating Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-5321149719702676783</id><published>2013-07-01T00:37:00.000+05:30</published><updated>2013-07-17T00:15:19.651+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="data types"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Being a programmer you will be tackling large heaps of data many a times, in php also, like storing the username, email, password and other sensitive data of large no. of users. Such kind of data sets also need to be manipulated, sorted and retrieved. Luckily php as many other programming languages, also provides you with the feature of ARRAY which provides you with the ideal way to store, manipulate, sort and retrieve data sets.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;You are already familiar with the &lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;array data type&lt;/a&gt;&lt;/b&gt;. In this series of lectures, arrays will be covered from basics of creating an array to merging and slicing an array. After this series of lectures, specifically, you will be able to do these things namely:&lt;/span&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/creating-arrays.html&quot;&gt;Create arrays&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/outputting-arrays.html&quot;&gt;Output arrays&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/outputting-arrays.html&quot;&gt;Test for an array&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/adding-and-removing-array-elements.html&quot;&gt;Add and remove array elements&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/locating-array-elements.html&quot;&gt;Locate array elements&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/traversing-arrays.html&quot;&gt;Traverse arrays&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/array-size-and-uniqueness.html&quot;&gt;Determine array size and element uniqueness&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/sorting-arrays.html&quot;&gt;Sort arrays&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt; &lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/07/merging-and-slicing-arrays.html&quot;&gt;Merge and slice&lt;/a&gt; || splice and dissect arrays&lt;/strong&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Before rushing to these topics, let me first introduce you to our not so new data type- &lt;em&gt;&lt;strong&gt;Array&lt;/strong&gt;&lt;/em&gt;.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
An array is traditionally defined as a group of items that share certain characteristics, such as similarity (car models, baseball teams, types of fruit, etc.) and type (e.g., all strings or integers). Each item is distinguished by a special identifier known as a key.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP takes this definition a step further, forgoing the requirement that the items share the same data type (more about data types). For example, an array could quite possibly contain items such as state names, ZIP codes, exam scores, or playing card suits. Each item consists of two components: the aforementioned key and a value. The &lt;em&gt;&lt;strong&gt;key&lt;/strong&gt;&lt;/em&gt; serves as the lookup facility for retrieving its counterpart, the &lt;strong&gt;&lt;em&gt;value&lt;/em&gt;&lt;/strong&gt;. Keys can be numerical or associative. Numerical keys bear no real relation to the value other than the value’s position in the array(Like many other programming languages, PHP’s numerically indexed arrays begins with position 0, and not 1).&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
As an example, the array could consist of an alphabetically sorted list of state names, with key 0 representing Alabama and key 49 representing Wyoming. Using PHP syntax, this might look like the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(0 =&amp;gt; &quot;Alabama&quot;, 1 =&amp;gt; &quot;Alaska&quot;...49 =&amp;gt; &quot;Wyoming&quot;);&lt;br /&gt;
// $states[0] = “Alabama”&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Whereas an associative key logically bears a direct relation to its corresponding value. Mapping arrays associatively is particularly convenient when using numerical index values just doesn’t make sense. For instance, you might want to create an array that maps state abbreviations to their names. Using PHP syntax, this might look like the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array(&quot;HR&quot; =&amp;gt; &quot;Haryana&quot;, &quot;HP&quot; =&amp;gt; &quot;Himachal Pradesh&quot;, &quot;NY&quot; =&amp;gt; &quot;New York&quot;)&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
You could then reference Haryana like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states[&quot;HR&quot;]&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;em&gt;It’s also possible to create arrays of arrays, known as &lt;strong&gt;multidimensional arrays&lt;/strong&gt;&lt;/em&gt;. For example, you could use a multidimensional array to store the state information. Using PHP syntax, it might look like this:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states = array (&lt;br /&gt;
&quot;Haryana&quot; =&amp;gt; array(&quot;population&quot; =&amp;gt; &quot;11,353,140&quot;, &quot;capital&quot; =&amp;gt; &quot;Chandigarh&quot;),&lt;br /&gt;
&quot;Punjab&quot; =&amp;gt; array(&quot;population&quot; =&amp;gt; &quot;1,711,263&quot;, &quot;capital&quot; =&amp;gt; &quot;Chandigarh&quot;) // capital of Haryana and Punjab is same&lt;br /&gt;
);&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You could then reference Haryana’s population:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$states[&quot;Haryana&quot;][&quot;population&quot;]&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
This would return the following:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
11,353,140&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:&lt;/strong&gt; For the above example, Haryana and Punjab are the keys in the array $states whereas population and capital are the keys in both arrays Haryana and Punjab.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;http://letusdophp.blogspot.in/2013/07/creating-arrays.html&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;Creating Arrays&lt;/i&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/5321149719702676783/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/07/arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5321149719702676783'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5321149719702676783'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/07/arrays.html' title='Arrays'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-6579107200119458328</id><published>2013-06-30T00:36:00.000+05:30</published><updated>2013-06-30T17:00:43.196+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="function"/><category scheme="http://www.blogger.com/atom/ns#" term="function libraries"/><category scheme="http://www.blogger.com/atom/ns#" term="function parameters"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="recursive functions"/><title type='text'>Functions in php – Part 4</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1&gt;
Recursive Functions&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Recursive functions, or &lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;functions&lt;/a&gt;&lt;/b&gt; that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved. Practically every introductory recursion example involves factorial computation. Let’s do something a tad more practical and create a loan payment calculator. Specifically, the following example uses recursion to create a payment schedule, telling you the principal and interest amounts required of each payment installment to repay the loan. The recursive function, amortizationTable(), is introduced in script 1. It takes as input four arguments: $pNum, which identifies the payment number; $periodicPayment, which carries the total monthly payment; $balance, which indicates the remaining loan balance; and $monthlyInterest, which determines the monthly interest percentage rate. These items are designated or determined in the script 2. &lt;/div&gt;
&lt;br /&gt;
Script 1. The Payment Calculator Function, amortizationTable()&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
function amortizationTable($pNum, $periodicPayment, $balance, $monthlyInterest)&lt;br /&gt;{&lt;br /&gt;// Calculate payment interest&lt;br /&gt;
$paymentInterest = round($balance * $monthlyInterest, 2);&lt;br /&gt;// Calculate payment principal&lt;br /&gt;$paymentPrincipal = round($periodicPayment - $paymentInterest, 2);&lt;br /&gt;// Deduct principal from remaining balance&lt;br /&gt;$newBalance = round($balance - $paymentPrincipal, 2);&lt;br /&gt;// If new balance &amp;lt; monthly payment, set to zero&lt;br /&gt;if ($newBalance &amp;lt; $paymentPrincipal) {&lt;br /&gt;$newBalance = 0;&lt;br /&gt;}&lt;br /&gt;printf(&quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;%d&amp;lt;/td&amp;gt;&quot;, $pNum);&lt;br /&gt;printf(&quot;&amp;lt;td&amp;gt;$%s&amp;lt;/td&amp;gt;&quot;, number_format($newBalance, 2));&lt;br /&gt;printf(&quot;&amp;lt;td&amp;gt;$%s&amp;lt;/td&amp;gt;&quot;, number_format($periodicPayment, 2));&lt;br /&gt;printf(&quot;&amp;lt;td&amp;gt;$%s&amp;lt;/td&amp;gt;&quot;, number_format($paymentPrincipal, 2));&lt;br /&gt;printf(&quot;&amp;lt;td&amp;gt;$%s&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&quot;, number_format($paymentInterest, 2));&lt;br /&gt;# If balance not yet zero, recursively call amortizationTable()&lt;br /&gt;if ($newBalance &amp;gt; 0) {&lt;br /&gt;$pNum++;&lt;br /&gt;amortizationTable($pNum, $periodicPayment,&lt;br /&gt;$newBalance, $monthlyInterest);&lt;br /&gt;} else {&lt;br /&gt;return 0;&lt;br /&gt;}&lt;br /&gt;}&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
After setting pertinent variables and performing a few preliminary calculations, script 2 invokes the amortizationTable() function. Because this function calls itself recursively, all amortization table calculations will be performed internal to this function; once complete, control is returned to the caller.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Script 2. A Payment Schedule Calculator Using Recursion&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;// Loan balance&lt;br /&gt;$balance = 10000.00;&lt;br /&gt;// Loan interest rate&lt;br /&gt;$interestRate = .0575;&lt;br /&gt;// Monthly interest rate&lt;br /&gt;$monthlyInterest = $interestRate / 12;&lt;br /&gt;// Term length of the loan, in years.&lt;br /&gt;$termLength = 5;&lt;br /&gt;// Number of payments per year.&lt;br /&gt;$paymentsPerYear = 12;&lt;br /&gt;
// Payment iteration&lt;br /&gt;$paymentNumber = 1;&lt;br /&gt;// Determine total number payments&lt;br /&gt;$totalPayments = $termLength * $paymentsPerYear;&lt;br /&gt;// Determine interest component of periodic payment&lt;br /&gt;$intCalc = 1 + $interestRate / $paymentsPerYear;&lt;br /&gt;// Determine periodic payment&lt;br /&gt;$periodicPayment = $balance * pow($intCalc,$totalPayments) * ($intCalc - 1) / (pow($intCalc,$totalPayments) - 1);&lt;br /&gt;// Round periodic payment to two decimals&lt;br /&gt;$periodicPayment = round($periodicPayment,2);&lt;br /&gt;// Create table&lt;br /&gt;echo &quot;&amp;lt;table width=&#39;50%&#39; align=&#39;center&#39; border=&#39;1&#39;&amp;gt;&quot;;&lt;br /&gt;echo &quot;&amp;lt;tr&amp;gt;&lt;br /&gt;&amp;lt;th&amp;gt;Payment Number&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Balance&amp;lt;/th&amp;gt;&lt;br /&gt;&amp;lt;th&amp;gt;Payment&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Principal&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Interest&amp;lt;/th&amp;gt;&lt;br /&gt;&amp;lt;/tr&amp;gt;&quot;;&lt;br /&gt;// Call recursive function&lt;br /&gt;amortizationTable($paymentNumber, $periodicPayment, $balance, $monthlyInterest);&lt;br /&gt;// Close table&lt;br /&gt;echo &quot;&amp;lt;/table&amp;gt;&quot;;&lt;br /&gt;?&amp;gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Figure below shows sample output, based on monthly payments made on a five-year fixed loan of $10,000.00 at 5.75 percent interest. For reasons of space conservation, just the first 12 payment iterations are listed.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;a href=&quot;http://lh6.ggpht.com/-QunxFOvudf4/Uc_v-7Kn_OI/AAAAAAAAAG8/MZwVXgPJ3yQ/s1600-h/recursivevfunctions_thumb92.png&quot;&gt;&lt;img alt=&quot;recursivevfunctions_thumb[9]&quot; border=&quot;0&quot; height=&quot;659&quot; src=&quot;http://lh3.ggpht.com/-nSh8LZgIkLQ/Uc_wIR4SkOI/AAAAAAAAAHE/qqEVEfyLlu0/recursivevfunctions_thumb9_thumb.png?imgmax=800&quot; style=&quot;background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot; title=&quot;recursivevfunctions_thumb[9]&quot; width=&quot;725&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Function Libraries&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Personally I feel this is the most important part of functions. &lt;em&gt;&lt;u&gt;Great programmers are lazy, and lazy programmers think in terms of reusability.&lt;/u&gt;&lt;/em&gt; Functions offer a great way to reuse code and are often collectively assembled into libraries and subsequently repeatedly reused within similar applications. PHP libraries are created via the simple aggregation of function definitions in a single file, like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;function localTax($grossIncome, $taxRate) {&lt;br /&gt;// function body here&lt;br /&gt;}&lt;br /&gt;function stateTax($grossIncome, $taxRate, $age) {&lt;br /&gt;// function body here&lt;br /&gt;}&lt;br /&gt;function medicare($grossIncome, $medicareRate) {&lt;br /&gt;// function body here&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
Save this library, preferably using a naming convention that will clearly denote its purpose, such as taxes.library.php. Do not, however, save this file within the server document root using an extension that would cause the web server to pass the file contents unparsed. Doing so opens up the possibility for a user to call the file from the browser and review the code, which could contain sensitive data. You can insert this file into scripts using &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-8-control-structures-4.html&quot;&gt;include(), include_once(), require(), or require_once()&lt;/a&gt;&lt;/strong&gt;. (Alternatively, you could use PHP’s &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/phps-configuration-directives-part-3.html&quot;&gt;auto_prepend&lt;/a&gt;&lt;/strong&gt; &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/configuring-php.html&quot;&gt;configuration directive&lt;/a&gt;&lt;/strong&gt; to automate the task of file insertion for you.) For example, assuming that you titled this library taxation.library.php, you could include it into a script like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;require_once(&quot;taxation.library.php&quot;);&lt;br /&gt;...&lt;br /&gt;?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
Once included, any of the three functions found in this library can be invoked (&lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;invoking functions&lt;/a&gt;&lt;/b&gt;) as needed.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-3.html&quot;&gt;Functions in php - Part 3&lt;/a&gt;&amp;nbsp;&amp;lt;-&amp;gt; &lt;a href=&quot;http:/#&quot;&gt;ARRAYS&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/6579107200119458328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6579107200119458328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/6579107200119458328'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-4.html' title='Functions in php – Part 4'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-nSh8LZgIkLQ/Uc_wIR4SkOI/AAAAAAAAAHE/qqEVEfyLlu0/s72-c/recursivevfunctions_thumb9_thumb.png?imgmax=800" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.970902261018843 76.74774169921875</georss:point><georss:box>28.859681261018842 76.586380199218752 29.082123261018843 76.909103199218748</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-7535671813551555683</id><published>2013-06-29T00:35:00.000+05:30</published><updated>2013-06-30T16:46:57.790+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="function"/><category scheme="http://www.blogger.com/atom/ns#" term="function parameters"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="return statement"/><category scheme="http://www.blogger.com/atom/ns#" term="using type hinting"/><title type='text'>Functions in php – Part 3</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1&gt;
Using Type Hinting&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
PHP 5 introduced a new feature known as &lt;strong&gt;Type hinting&lt;/strong&gt;, which gives you the ability to force parameters to be objects of a certain class or to be arrays. Unfortunately, type hinting using &lt;b&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;scalar data types&lt;/a&gt;&lt;/b&gt; such as integers and strings is not supported. If the provided parameter is not of the desired type, a fatal error will occur. As an example, suppose you created a class named Customer and wanted to be certain that any parameter passed to a function named processPayPalPayment() was of type Customer. You could use type hinting to implement this restriction:&lt;/div&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;function processPayPalPayment(Customer $customer) {&lt;br /&gt;// Process the customer&#39;s payment&lt;br /&gt;}&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1&gt;
Returning Values from a Function&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Often, simply relying on a function to do something is insufficient; a script’s outcome might depend on a function’s outcome or on changes in data resulting from its execution. Yet variable scoping prevents information from easily being passed from a function body back to its caller, so how can we accomplish this? You can pass data back to the caller by way of the return() statement.&lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
The return Statement&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
The return() statement returns any ensuing value back to the function caller, returning program control back to the caller’s scope in the process. If return() is called from within the global scope, the script execution is terminated. Revising the calcSalestax() function again, suppose you don’t want to immediately &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/php-basics-part-3-outputting-data.html&quot;&gt;echo&lt;/a&gt;&lt;/strong&gt; the sales total back to the user upon calculation, but rather want to return the value to the calling block:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
function calcSalesTax($price, $tax=.0675)&lt;br /&gt;
{&lt;br /&gt;
$total = $price + ($price * $tax);&lt;br /&gt;
return $total;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Alternatively, you could return the calculation directly without even assigning it to $total, like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
function calcSalesTax($price, $tax=.0675)&lt;br /&gt;
{&lt;br /&gt;
return $price + ($price * $tax);&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
Here’s an example of how you would call this function:&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$price = 6.99;&lt;br /&gt;
$total = calcSalesTax($price);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
To return a reference from a function, use the reference operator &amp;amp; in both the function declaration and when assigning the returned value to a variable(&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot;&gt;&lt;b&gt;more about variables in php&lt;/b&gt;&lt;/a&gt;):  &lt;/div&gt;
&lt;br /&gt;
Example #3 Returning a reference from a function  &lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
function &amp;amp;returns_reference()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return $someref;&lt;br /&gt;
}&lt;br /&gt;
$newref =&amp;amp; returns_reference();&lt;br /&gt;
?&amp;gt; &lt;/div&gt;
&lt;br /&gt;
&lt;h1&gt;
Returning Multiple Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
It’s often convenient to return multiple values from a function. For example, suppose that you’d like to create a function that retrieves user data from a database (say the user’s name, e-mail address, and phone number) and returns it to the caller. Accomplishing this is much easier than you might think, with the help of a very useful language construct, list(). The list() construct offers a convenient means for retrieving values from an array, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$colors = array(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;);&lt;br /&gt;
list($red, $blue, $green) = $colors;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
Once the list() construct executes, $red, $blue, and $green will be assigned red, blue, and green, respectively.&lt;br /&gt;
Building on the concept demonstrated in the previous example, you can imagine how the three prerequisite values might be returned from a function using list():&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
function retrieveUserProfile()&lt;br /&gt;
{&lt;br /&gt;
$user[] = &quot;Jason Gilmore&quot;;&lt;br /&gt;
$user[] = &quot;jason@example.com&quot;;&lt;br /&gt;
$user[] = &quot;English&quot;;&lt;br /&gt;
return $user;&lt;br /&gt;
}&lt;br /&gt;
list($name, $email, $language) = retrieveUserProfile();&lt;br /&gt;
echo &quot;Name: $name, email: $email, language: $language&quot;;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Executing this script returns the following:&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;php_output&quot;&gt;
Name: Jason Gilmore, email: jason@example.com, language: English&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This feature is quite useful and will be used repeatedly throughout your journey in php programming.&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-2.html&quot;&gt;Functions – Part 2&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-4.html&quot;&gt;Functions – Part 4&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/7535671813551555683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/7535671813551555683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/7535671813551555683'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-3.html' title='Functions in php – Part 3'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-5255905268261325825</id><published>2013-06-27T00:34:00.000+05:30</published><updated>2013-06-30T16:48:19.766+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="default argument values"/><category scheme="http://www.blogger.com/atom/ns#" term="function"/><category scheme="http://www.blogger.com/atom/ns#" term="function parameters"/><category scheme="http://www.blogger.com/atom/ns#" term="passing arguments by reference"/><category scheme="http://www.blogger.com/atom/ns#" term="passing arguments by value"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Functions in php – Part 2</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Passing Arguments by Value&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
You‘ll often find it useful to pass data into a &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;function&lt;/a&gt;&lt;/strong&gt;. As an example, let’s create a function that calculates an item’s total cost by determining its sales tax and then adding that amount to the price:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
function calcSalesTax($price, $tax)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $total = $price + ($price * $tax);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;Total cost: $total&quot;;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This function accepts two parameters, aptly named $price and $tax, which are used in the calculation. Although these parameters are intended to be &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;floating points&lt;/a&gt;&lt;/strong&gt;, because of PHP’s weak typing, nothing prevents you from passing in &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/variables-part-1-declaration.html&quot;&gt;variables&lt;/a&gt;&lt;/strong&gt; of any &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt;datatype&lt;/a&gt;&lt;/strong&gt;, but the outcome might not be what you expect. In addition, you’re allowed to define as few or as many parameters as you deem necessary; there are no language-imposed constraints in this regard.&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Once defined, you can then invoke the function as demonstrated in the &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;Functions in php - Part 1&lt;/a&gt;&lt;/strong&gt;. For example, the calcSalesTax() function would be called like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
calcSalesTax(15.00, .075);&lt;/div&gt;
&lt;br /&gt;
Of course, you’re not bound to passing static values into the function. You can also pass variables like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$pricetag = 15.00;&lt;br /&gt;
$salestax = .075;&lt;br /&gt;
calcSalesTax($pricetag, $salestax);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
When you pass an argument in this manner, it’s called &lt;strong&gt;passing by value&lt;/strong&gt;. This means that any changes made to those values within the scope of the function are ignored outside of the function because not the actual value but a copy of the value is passed into the function and when the fucntion ends this value is destroyed. Hence, there is no change to the actual value.&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Take this example for instance:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$num1= 10&lt;br /&gt;
$num2= 20&lt;br /&gt;
function something($num1,$num2)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $num1 += $num2;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;$num1&quot;;&lt;br /&gt;
}&lt;br /&gt;
something($num1,$num2);&lt;br /&gt;
echo &quot;$num1&quot;;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Output:&lt;/div&gt;
&lt;div id=&quot;php_output&quot;&gt;
30&lt;br /&gt;
10&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If you want these changes to be reflected outside of the function’s scope, you can pass the argument by reference, introduced next.&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Passing Arguments by Reference&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
On occasion, you may want any changes made to an argument within a function to be reflected outside of the function’s scope. Passing the argument by reference accomplishes this, because in this case a copy of the argument is not provided to function instead the argument itself is provided. Hence any change to the argument inside the function will be reflected outside of the function. Passing an argument by reference is done by appending an ampersand(&amp;amp;) to the front of the argument. Here’s an example:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$cost = 20.99;&lt;br /&gt;
$tax = 0.0575;&lt;br /&gt;
function calculateCost(&amp;amp;$cost, $tax)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Modify the $cost variable&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $cost = $cost + ($cost * $tax);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Perform some random change to the $tax variable.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $tax += 4;&lt;br /&gt;
}&lt;br /&gt;
calculateCost($cost, $tax);&lt;br /&gt;
printf(&quot;Tax is %01.2f%% &quot;, $tax*100);&lt;br /&gt;
printf(&quot;Cost is: $%01.2f&quot;, $cost);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Here’s the result:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Tax is 5.75%&lt;br /&gt;
Cost is $22.20&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note&lt;/strong&gt; the value of $tax remains the same, although $cost has changed.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Take this another example:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
function add_some_extra(&amp;amp;$string)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $string .= &#39;and something extra.&#39;;&lt;br /&gt;
}&lt;br /&gt;
http://letusdophp.blogspot.in/2013/06/functions-in-php-part-2.html
$str = &#39;This is a string, &#39;;&lt;br /&gt;
add_some_extra($str);&lt;br /&gt;
echo $str;&amp;nbsp;&amp;nbsp;&amp;nbsp; // outputs &#39;This is a string, and something extra.&#39;&lt;br /&gt;
?&amp;gt; &lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Ques. :&lt;/strong&gt; Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?&lt;br /&gt;
&lt;strong&gt;Ans. :&lt;/strong&gt; &quot;A copy of the original $array is created within the function scope. Once the function terminates, the scope is removed and the copy of $array with it.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Default Argument Values&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Default values can be assigned to input arguments, which will be automatically assigned to the argument if no other value is provided. To revise the sales tax example, suppose that for majority of your sales $tax is 6.75 percent :-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
function calcSalesTax($price, $tax=.0675)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $total = $price + ($price * $tax);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;Total cost: $total&quot;;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
You can still pass $tax another taxation rate; 6.75 percent will be used only if calcSalesTax() is invoked without the second parameter like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
$price = 15.47;&lt;br /&gt;
calcSalesTax($price);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Default argument values must appear at the end of the parameter list and must be constant expressions; you cannot assign non-constant values such as function calls or variables.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
function calcSalesTax($price, $tax=&quot;&quot;)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $total = $price + ($price * $tax);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;Total cost: $total&quot;;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This allows you to call calcSalesTax() without the second parameter if there is no sales tax:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
calcSalesTax(42.00);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following output:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
Total cost: $42&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Now consider this example:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
function calculate($price, $price2=&quot;&quot;, $price3=&quot;&quot;)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo $price + $price2 + $price3;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
You can then call calculate(), passing along just $price and $price3, like so:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
calculate(10, &quot;&quot;, 3);&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
This returns the following value:&lt;/div&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_output&quot;&gt;
13&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note&lt;/strong&gt; again that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-1.html&quot;&gt;Functions in php – Part 1&lt;/a&gt; &amp;lt;-&amp;gt; &lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-3.html&quot;&gt;Functions in php – Part 3&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/5255905268261325825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5255905268261325825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/5255905268261325825'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-2.html' title='Functions in php – Part 2'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry><entry><id>tag:blogger.com,1999:blog-2687042700778634400.post-2607633516255565203</id><published>2013-06-26T13:03:00.000+05:30</published><updated>2013-06-26T23:54:26.347+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="creating a function"/><category scheme="http://www.blogger.com/atom/ns#" term="function"/><category scheme="http://www.blogger.com/atom/ns#" term="function parameters"/><category scheme="http://www.blogger.com/atom/ns#" term="function within function"/><category scheme="http://www.blogger.com/atom/ns#" term="invoking a function"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Functions in php – Part 1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;a href=&quot;http://lh3.ggpht.com/-XTYu52WEFUw/Ucsqgw4AJLI/AAAAAAAAAGU/NIquUn7GfoQ/s1600-h/Capture%25255B3%25255D.png&quot;&gt;&lt;img alt=&quot;function&quot; height=&quot;297&quot; src=&quot;http://lh4.ggpht.com/-YJntM8MULQ0/Ucsqj4mBWTI/AAAAAAAAAGc/QPmrynKeMzw/Capture_thumb%25255B1%25255D.png?imgmax=800&quot; style=&quot;display: inline;&quot; title=&quot;function&quot; width=&quot;649&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
You’ll often find that some tasks are comprised of bits of logic which can be reused elsewhere, not only within the same application but also in many other applications. For example, an e-commerce application might need to validate an email address on several different pages, such as when a new user registers to use a Web site or when a visitor signs up for a newsletter. The logic used to validate an e-mail address is quite complex, and therefore it would be a smart act to maintain the logic in a single location rather than literally embed&amp;nbsp; it into numerous pages, particularly if it one day needs to be modified to account for a new domain.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
The concept of embodying these repetitive processes within a named section of code and then invoking this name when necessary has long been a key feature of modern computer languages which is known as a &lt;strong&gt;Function&lt;/strong&gt;, and it grants you the convenience of a singular point of reference if the process it defines requires changes in the future, which greatly reduces both the possibility of programming errors and maintenance overhead. &lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
In this series of lecture, we&#39;ll be covering each and every aspect of a Function, including how to create and invoke them, &lt;strong&gt;&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/functions-in-php-part-2.html&quot;&gt;pass input to them(by value and by reference)&lt;/a&gt;&lt;/strong&gt;, use a relatively new feature known as &lt;strong&gt;type hinting&lt;/strong&gt;, &lt;strong&gt;return both single and multiple values to the caller&lt;/strong&gt;, and &lt;strong&gt;create and include function libraries&lt;/strong&gt;. Additionally, you’ll learn about both &lt;strong&gt;recursive and variable functions&lt;/strong&gt;.&lt;/div&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; Keep an eye on each and every aspect of function coz it&#39;s quite important.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Invoking a Function&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
More than 1,000 functions are built into the standard PHP distribution and you can include any function defined elsewhere by &lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-8-control-structures-4.html&quot;&gt;&lt;strong&gt;include() or require()&lt;/strong&gt;.&lt;/a&gt; You can anytime invoke any of these functions by just specifying their names. For example, to raise five to the third power:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$value = pow(5,3); // returns 125&lt;br /&gt;
echo $value;&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; pow is predefined library function termed as power. pow(x,y) will raise x to the y power&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
In the example above, you can bypass assigning the value to a variable and simply get an output, like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
echo pow(5,3);&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Output:&lt;/div&gt;
&lt;div id=&quot;php_output&quot;&gt;
125&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
If you want to output the function outcome within a larger&lt;a href=&quot;http://letusdophp.blogspot.in/2013/06/php-basics-part-4-phps-supported-data.html&quot;&gt; &lt;strong&gt;string&lt;/strong&gt;,&lt;/a&gt; you can do this as :-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
printf(&quot;Five raised to the power three is %d.&quot;, pow(5,3));&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Output:&lt;/div&gt;
&lt;div id=&quot;php_output&quot;&gt;
Five raised to the power three is 125.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; Refer to the &lt;strong&gt;&lt;a href=&quot;http://php.net/&quot;&gt;php documentation&lt;/a&gt;&lt;/strong&gt; for complete list of predefined functions.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Creating a Function&lt;/h1&gt;
&lt;div align=&quot;justify&quot;&gt;
Although php offers a vast variety of predefined functions but sooner or later you will feel the need to go beyond these predefined functions and create custom functions or even entire function libraries.&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
You can easily create a function as follows:&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;function functionName(parameters)&lt;br /&gt;{&lt;br /&gt;function-body&lt;br /&gt;}&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Take an example:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
function helloworld()&lt;br /&gt;
{&lt;br /&gt;
echo &quot;Hello world!&quot;;&lt;br /&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Once defined you can call this function as follows:&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
helloworld();&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;br /&gt;
Output:&lt;/div&gt;
&lt;div id=&quot;php_output&quot;&gt;
Hello world!&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; In the example above there are no parameters. Parameters are like variables in the function and there could be none or any number of parameters inside a function.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
Now take this example:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;PHP Function with Parameters&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
function addFunction($num1, $num2)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $sum = $num1 + $num2;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;Sum of the two numbers is : $sum&quot;;&lt;br /&gt;
}&lt;br /&gt;
addFunction(10, 20);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Output:&lt;br /&gt;
&lt;a href=&quot;http://lh6.ggpht.com/-NsHPxSlGIJ4/Ucsqlx2X3gI/AAAAAAAAAGk/esc5GsMy4_8/s1600-h/parameters%25255B22%25255D.png&quot;&gt;&lt;img alt=&quot;function parameters&quot; height=&quot;328&quot; src=&quot;http://lh3.ggpht.com/-Kb_tCeVIqkk/UcsqpRLe8HI/AAAAAAAAAGs/K9NiyymU0fE/parameters_thumb%25255B20%25255D.png?imgmax=800&quot; style=&quot;display: inline;&quot; title=&quot;function parameters&quot; width=&quot;670&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
&lt;div align=&quot;justify&quot;&gt;
Most of the times it is not necessary to define a function before it is called except when it is conditionally defined. This is because of the fact that php looks over the entire script into engine before executing it. BUT THIS IS HAPHAZARD PRACTICE IS NOT RECOMMENDED. SO DON&#39;T TRY THIS.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot;&gt;
Take this as example:-&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
$makefoo = true;&lt;br /&gt;
/* We can&#39;t call function foo() from here &lt;br /&gt;
&amp;nbsp;&amp;nbsp; since it doesn&#39;t exist yet,&lt;br /&gt;
&amp;nbsp;&amp;nbsp; but we can call bar() */&lt;br /&gt;
bar();&lt;br /&gt;
if ($makefoo) {&lt;br /&gt;
&amp;nbsp; function foo()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;I don&#39;t exist until program execution reaches me.\n&quot;;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
/* Now we can safely call foo()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; since $makefoo evaluated to true */&lt;br /&gt;
if ($makefoo) foo();&lt;br /&gt;
function bar() &lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; echo &quot;I exist immediately upon program start.\n&quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h1 align=&quot;justify&quot;&gt;
Function within function:-&lt;/h1&gt;
&lt;br /&gt;
&lt;div align=&quot;justify&quot; id=&quot;php_code&quot;&gt;
&amp;lt;?php&lt;br /&gt;
function foo() &lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; function bar() &lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;I don&#39;t exist until foo() is called.\n&quot;;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
/* We can&#39;t call bar() yet&lt;br /&gt;
&amp;nbsp;&amp;nbsp; since it doesn&#39;t exist. */ &lt;br /&gt;
foo();&lt;br /&gt;
/* Now we can call bar(),&lt;br /&gt;
&amp;nbsp;&amp;nbsp; foo()&#39;s processing has&lt;br /&gt;
&amp;nbsp;&amp;nbsp; made it accessible. */&lt;br /&gt;
bar();&lt;br /&gt;
?&amp;gt; &lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;em&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://letusdophp.blogspot.com/2013/06/functions-in-php-part-2.html&quot;&gt;Functions in php - Part 2&lt;/a&gt;&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://letusdophp.blogspot.com/feeds/2607633516255565203/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/2607633516255565203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2687042700778634400/posts/default/2607633516255565203'/><link rel='alternate' type='text/html' href='http://letusdophp.blogspot.com/2013/06/functions-in-php-part-1.html' title='Functions in php – Part 1'/><author><name>Ankush</name><uri>http://www.blogger.com/profile/06859217960226664466</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-YJntM8MULQ0/Ucsqj4mBWTI/AAAAAAAAAGc/QPmrynKeMzw/s72-c/Capture_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Rohtak, Haryana, India</georss:featurename><georss:point>28.892361 76.591239999999971</georss:point><georss:box>28.78114 76.429878499999973 29.003582 76.752601499999969</georss:box></entry></feed>