<?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: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-7781560989272961089</id><updated>2012-02-16T01:03:59.429-08:00</updated><title type='text'>PHP Programming</title><subtitle type='html'>php programming,MYSQL programming,Magento programming,Ajax,XML,Javascripts,ASP,System adminsitration,Linux,Freebsd,XML,SVN commands,joomla tutorial,
Nepalihost,Iwebnepal Private Limited

Web designing Nepal,Domain Registration Nepal,Web Hosting Nepal,Web Host Nepal,Web Designing Nepal,Domain Host Nepal,Dedicated Hosting Nepal,Web Design Nepal,Domain registration .np,Web Nepal,Cpanel Demo,WHM pannel</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>60</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-801196842154528578</id><published>2010-05-05T13:08:00.001-07:00</published><updated>2010-05-05T13:08:36.939-07:00</updated><title type='text'>Flickr</title><content type='html'>This is a test post from &lt;a href="http://www.flickr.com/r/testpost"&gt;&lt;img alt="flickr" src="http://www.flickr.com/images/flickr_logo_blog.gif" width="41" height="18" border="0" align="absmiddle" /&gt;&lt;/a&gt;, a fancy photo sharing thing.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-801196842154528578?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/801196842154528578/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/05/flickr.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/801196842154528578'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/801196842154528578'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/05/flickr.html' title='Flickr'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4266476194819063745</id><published>2010-03-31T00:07:00.000-07:00</published><updated>2010-03-31T00:08:21.491-07:00</updated><title type='text'>PHP Tutorial  User Registration SQL</title><content type='html'>A quick and easy user registration using php, sql and phpmyadmin.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For full size and source codes: http://www.neoblob.com/phpsquad/tuts/...&lt;br /&gt;&lt;br /&gt;SQL:&lt;br /&gt;CREATE TABLE `user` (&lt;br /&gt;`id` int(10) unsigned NOT NULL auto_increment,&lt;br /&gt;`username` varchar(20) NOT NULL default '',&lt;br /&gt;`password` varchar(50) NOT NULL default '',&lt;br /&gt;`email` varchar(40) NOT NULL default '',&lt;br /&gt;`ip` varchar(20) NOT NULL default '',&lt;br /&gt;PRIMARY KEY (`id`)&lt;br /&gt;) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;&lt;br /&gt;&lt;br /&gt;&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/OQ14NjbEXJM&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/OQ14NjbEXJM&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4266476194819063745?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4266476194819063745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-user-registration-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4266476194819063745'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4266476194819063745'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-user-registration-sql.html' title='PHP Tutorial  User Registration SQL'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3586574159663741241</id><published>2010-03-31T00:00:00.001-07:00</published><updated>2010-03-31T00:01:12.190-07:00</updated><title type='text'>Make A Website Publicly accessible with Port Forwarding Linksys Router networknerd007 41 videos</title><content type='html'>This tutorial shows how to make your Apache/XAMPP for Windows XP/Vista or Linux Ubuntu web server accessible to the public people over the internet by properly configuring and forwarding your ports through your router. I use a Linksys WRT54G in this tutorial, but it should be really close to the same on all Linksys routers and fairly familiar on other brands as well.&lt;br /&gt;&lt;br /&gt;&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/pIK-RpVNAM8&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/pIK-RpVNAM8&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3586574159663741241?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3586574159663741241/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/make-website-publicly-accessible-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3586574159663741241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3586574159663741241'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/make-website-publicly-accessible-with.html' title='Make A Website Publicly accessible with Port Forwarding Linksys Router networknerd007 41 videos'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4208028940480426624</id><published>2010-03-30T23:58:00.000-07:00</published><updated>2010-03-30T23:59:22.368-07:00</updated><title type='text'>Build a Web Form in Dreamweaver!</title><content type='html'>In the following video we will build a web based form using html. We will cover using Labels, Text Fields, Text Boxes, Radio Button Groups, Checkboxes, The Clear and Submit buttons, Drop down Menus, as well as many of the properties you can edit while building the form. The form will be ready to be linked with a PHP file for use when you are finished with this video. Get ready to learn a whole lot! Have fun and check out http://www.tutvid.com for more great videos and downloads!&lt;br /&gt;&lt;br /&gt;&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/7soyO5DwJMY&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/7soyO5DwJMY&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4208028940480426624?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4208028940480426624/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/build-web-form-in-dreamweaver.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4208028940480426624'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4208028940480426624'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/build-web-form-in-dreamweaver.html' title='Build a Web Form in Dreamweaver!'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4043272852894253196</id><published>2010-03-30T23:29:00.000-07:00</published><updated>2010-03-30T23:53:53.409-07:00</updated><title type='text'>PHP Tutorial - 23 - Adding Tables to MySQL Database</title><content type='html'>&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/ylKflDNWRLQ&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/ylKflDNWRLQ&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4043272852894253196?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4043272852894253196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-23-adding-tables-to-mysql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4043272852894253196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4043272852894253196'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-23-adding-tables-to-mysql.html' title='PHP Tutorial - 23 - Adding Tables to MySQL Database'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5850373392564738142</id><published>2010-03-30T23:28:00.000-07:00</published><updated>2010-03-30T23:43:00.759-07:00</updated><title type='text'>PHP &amp; MySQL Tutorial 08: logging in with MySQL Part 1</title><content type='html'>This tutorial goes over the concept of a login system in PHP while using sessions(to maintain the login status) and MySQL(a database that holds the information).&lt;br /&gt;[click more info to see link to sources online]&lt;br /&gt;(Hint: don't forget about Part 2 http://www.youtube.com/watch?v=ir12sx... I first go over how to make the login page, then the table in the database. After that, I make a user through PHPMyAdmin(the next tutorial will likely go over registering users). I then code how to validate that user and if they are validated, set the session information. After the session information is acceptable, they can go to a "members only" page which only allows identified people in. Then a way to log out.&lt;br /&gt;This tutorial is pretty much a walkthrough in concept of a mysql login system.&lt;br /&gt;&lt;object width="400" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/epo2qPGxZiE&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/epo2qPGxZiE&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5850373392564738142?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5850373392564738142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-mysql-tutorial-08-logging-in-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5850373392564738142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5850373392564738142'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-mysql-tutorial-08-logging-in-with.html' title='PHP &amp; MySQL Tutorial 08: logging in with MySQL Part 1'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3510490711980531598</id><published>2010-03-30T23:26:00.000-07:00</published><updated>2010-03-30T23:54:57.270-07:00</updated><title type='text'>PHP Basics: Install a Webserver with PHP and MySQL (Windows)</title><content type='html'>Get started with PHP by installing a webserver with PHP and MySQL support. For ease of use and convenience, i'll be showing you how to install XAMPP available from http://www.apachefriends.org/en/xampp...&lt;br /&gt;&lt;br /&gt;&lt;object width="400" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/IEJB7DkP7A8&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/IEJB7DkP7A8&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3510490711980531598?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3510490711980531598/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-basics-install-webserver-with-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3510490711980531598'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3510490711980531598'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-basics-install-webserver-with-php.html' title='PHP Basics: Install a Webserver with PHP and MySQL (Windows)'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7791919191479793449</id><published>2010-03-29T04:39:00.001-07:00</published><updated>2010-03-30T23:55:33.749-07:00</updated><title type='text'>PHP Mysql Tutorial</title><content type='html'>&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/ZdOoWwhEOyQ&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/ZdOoWwhEOyQ&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7791919191479793449?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7791919191479793449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-mysql-tutorial.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7791919191479793449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7791919191479793449'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-mysql-tutorial.html' title='PHP Mysql Tutorial'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-591805117672970136</id><published>2010-03-29T04:38:00.000-07:00</published><updated>2010-03-30T23:55:50.140-07:00</updated><title type='text'>Flash PHP MySQL CMS - Content Management System Website Tutorial CS3 + CS4</title><content type='html'>&lt;object width="640" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/pxHX6DmhWdA&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/pxHX6DmhWdA&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-591805117672970136?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/591805117672970136/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/flash-php-mysql-cms-content-management.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/591805117672970136'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/591805117672970136'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/flash-php-mysql-cms-content-management.html' title='Flash PHP MySQL CMS - Content Management System Website Tutorial CS3 + CS4'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8053034248266623842</id><published>2010-03-29T04:28:00.001-07:00</published><updated>2010-03-30T23:57:00.051-07:00</updated><title type='text'>PHP Tutorial - 24 - Connecting to MySQL Database</title><content type='html'>wanna watch this tutorial in high def? http://www.youtube.com/watc... there ya go! and don't forget to subscri...&lt;br /&gt;&lt;object width="480" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/TteaqgZ3l7c&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/TteaqgZ3l7c&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8053034248266623842?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8053034248266623842/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-24-connecting-to-mysql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8053034248266623842'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8053034248266623842'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorial-24-connecting-to-mysql.html' title='PHP Tutorial - 24 - Connecting to MySQL Database'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4987486699145570088</id><published>2010-03-29T04:27:00.001-07:00</published><updated>2010-03-30T23:56:08.712-07:00</updated><title type='text'>PHP Tutorials: MySQL (Part 1)</title><content type='html'>&lt;object width="300" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/Oo6iea2LKM0&amp;hl=en_US&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/Oo6iea2LKM0&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;span class="watch-expander-head-content"&gt;      &lt;span&gt;Using phpMyAdmin to create a new table on a database. The basics!&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4987486699145570088?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4987486699145570088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorials-mysql-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4987486699145570088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4987486699145570088'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/php-tutorials-mysql-part-1.html' title='PHP Tutorials: MySQL (Part 1)'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7656188025057048835</id><published>2010-03-29T04:26:00.000-07:00</published><updated>2010-03-29T04:31:39.787-07:00</updated><title type='text'>Insert data Into Mysql Using PHP - Tutorial</title><content type='html'>A tutorial from - www.Pic2res.com How to Insert data Into Mysql Using PHP , step by step tutorial for beginners&lt;br /&gt;&lt;object width="300" height="385"&gt;&lt;param name="movie" value="http://www.youtube.com/v/-h98LOzFJPE&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;embed src="http://www.youtube.com/v/-h98LOzFJPE&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7656188025057048835?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7656188025057048835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/insert-data-into-mysql-using-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7656188025057048835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7656188025057048835'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/insert-data-into-mysql-using-php.html' title='Insert data Into Mysql Using PHP - Tutorial'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7504678359977639217</id><published>2010-03-19T01:10:00.001-07:00</published><updated>2010-03-19T01:10:47.135-07:00</updated><title type='text'>Array Operators</title><content type='html'>Array Operators&lt;br /&gt;&lt;br /&gt;Example Name Result &lt;br /&gt;$a + $b Union Union of $a and $b. &lt;br /&gt;$a == $b Equality TRUE if $a and $b have the same key/value pairs. &lt;br /&gt;$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types. &lt;br /&gt;$a != $b Inequality TRUE if $a is not equal to $b. &lt;br /&gt;$a &lt;&gt; $b Inequality TRUE if $a is not equal to $b. &lt;br /&gt;$a !== $b Non-identity TRUE if $a is not identical to $b. &lt;br /&gt;&lt;br /&gt;The + operator appends the right handed array to the left handed, whereas duplicated keys are NOT overwritten. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = array("a" =&gt; "apple", "b" =&gt; "banana");&lt;br /&gt;$b = array("a" =&gt; "pear", "b" =&gt; "strawberry", "c" =&gt; "cherry");&lt;br /&gt;&lt;br /&gt;$c = $a + $b; // Union of $a and $b&lt;br /&gt;echo "Union of \$a and \$b: \n";&lt;br /&gt;var_dump($c);&lt;br /&gt;&lt;br /&gt;$c = $b + $a; // Union of $b and $a&lt;br /&gt;echo "Union of \$b and \$a: \n";&lt;br /&gt;var_dump($c);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When executed, this script will print the following: Union of $a and $b:&lt;br /&gt;array(3) {&lt;br /&gt;  ["a"]=&gt;&lt;br /&gt;  string(5) "apple"&lt;br /&gt;  ["b"]=&gt;&lt;br /&gt;  string(6) "banana"&lt;br /&gt;  ["c"]=&gt;&lt;br /&gt;  string(6) "cherry"&lt;br /&gt;}&lt;br /&gt;Union of $b and $a:&lt;br /&gt;array(3) {&lt;br /&gt;  ["a"]=&gt;&lt;br /&gt;  string(4) "pear"&lt;br /&gt;  ["b"]=&gt;&lt;br /&gt;  string(10) "strawberry"&lt;br /&gt;  ["c"]=&gt;&lt;br /&gt;  string(6) "cherry"&lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Elements of arrays are equal for the comparison if they have the same key and value. &lt;br /&gt;&lt;br /&gt;????? 15-5. Comparing arrays&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = array("apple", "banana");&lt;br /&gt;$b = array(1 =&gt; "banana", "0" =&gt; "apple");&lt;br /&gt;&lt;br /&gt;var_dump($a == $b); // bool(true)&lt;br /&gt;var_dump($a === $b); // bool(false)&lt;br /&gt;?&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7504678359977639217?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7504678359977639217/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/array-operators.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7504678359977639217'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7504678359977639217'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/array-operators.html' title='Array Operators'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5129537660571724490</id><published>2010-03-19T01:09:00.000-07:00</published><updated>2010-03-19T01:10:04.878-07:00</updated><title type='text'>array_merge_recursive</title><content type='html'>(PHP 4 &gt;= 4.0.1, PHP 5)&lt;br /&gt;&lt;br /&gt;array_merge_recursive -- Merge two or more arrays recursively&lt;br /&gt;Description&lt;br /&gt;array array_merge_recursive ( array array1 [, array ...] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. &lt;br /&gt;&lt;br /&gt;If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. &lt;br /&gt;&lt;br /&gt;????? 1. array_merge_recursive() example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$ar1 = array("color" =&gt; array("favorite" =&gt; "red"), 5);&lt;br /&gt;$ar2 = array(10, "color" =&gt; array("favorite" =&gt; "green", "blue"));&lt;br /&gt;$result = array_merge_recursive($ar1, $ar2);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The $result will be: &lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [color] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [favorite] =&gt; Array&lt;br /&gt;                (&lt;br /&gt;                    [0] =&gt; red&lt;br /&gt;                    [1] =&gt; green&lt;br /&gt;                )&lt;br /&gt;&lt;br /&gt;            [0] =&gt; blue&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [0] =&gt; 5&lt;br /&gt;    [1] =&gt; 10&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5129537660571724490?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5129537660571724490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/arraymergerecursive.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5129537660571724490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5129537660571724490'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/arraymergerecursive.html' title='array_merge_recursive'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-9133001304116517087</id><published>2010-03-19T01:08:00.000-07:00</published><updated>2010-03-19T01:09:21.377-07:00</updated><title type='text'>dns_get_record</title><content type='html'>PHP 5)&lt;br /&gt;&lt;br /&gt;dns_get_record --  Fetch DNS Resource Records associated with a hostname &lt;br /&gt;Description&lt;br /&gt;array dns_get_record ( string hostname [, int type [, array &amp;authns, array &amp;addtl]] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: This function is not implemented on Windows platforms, nor does it (currently) work on *BSD systems. Try the PEAR class Net_DNS. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This function returns an array of associative arrays. Each associative array contains at minimum the following keys: &lt;br /&gt;&lt;br /&gt;???? 1. Basic DNS attributes&lt;br /&gt;&lt;br /&gt;Attribute Meaning &lt;br /&gt;host The record in the DNS namespace to which the rest of the associated data refers.  &lt;br /&gt;class dns_get_record() only returns Internet class records and as such this parameter will always return IN.  &lt;br /&gt;type String containing the record type. Additional attributes will also be contained in the resulting array dependant on the value of type. See table below.  &lt;br /&gt;ttl Time To Live remaining for this record. This will not equal the record's original ttl, but will rather equal the original ttl minus whatever length of time has passed since the authoritative name server was queried.  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;hostname should be a valid DNS hostname such as "www.example.com". Reverse lookups can be generated using in-addr.arpa notation, but gethostbyaddr() is more suitable for the majority of reverse lookups. &lt;br /&gt;&lt;br /&gt;By default, dns_get_record() will search for any resource records associated with hostname. To limit the query, specify the optional type parameter. type may be any one of the following: DNS_A, DNS_CNAME, DNS_HINFO, DNS_MX, DNS_NS, DNS_PTR, DNS_SOA, DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ALL or DNS_ANY. The default is DNS_ANY. &lt;br /&gt;&lt;br /&gt;????: Because of eccentricities in the performance of libresolv between platforms, DNS_ANY will not always return every record, the slower DNS_ALL will collect all records more reliably. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The optional third and fourth arguments to this function, authns and addtl are passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers, and any Additional Records respectively. See the example below. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;???? 2. Other keys in associative arrays dependant on 'type'&lt;br /&gt;&lt;br /&gt;Type Extra Columns &lt;br /&gt;A ip: An IPv4 addresses in dotted decimal notation.  &lt;br /&gt;MX pri: Priority of mail exchanger. Lower numbers indicate greater priority. target: FQDN of the mail exchanger. See also dns_get_mx().  &lt;br /&gt;CNAME target: FQDN of location in DNS namespace to which the record is aliased.  &lt;br /&gt;NS target: FQDN of the name server which is authoritative for this hostname.  &lt;br /&gt;PTR target: Location within the DNS namespace to which this record points.  &lt;br /&gt;TXT txt: Arbitrary string data associated with this record.  &lt;br /&gt;HINFO cpu: IANA number designating the CPU of the machine referenced by this record. os: IANA number designating the Operating System on the machine referenced by this record. See IANA's Operating System Names for the meaning of these values.  &lt;br /&gt;SOA mname: FQDN of the machine from which the resource records originated. rname: Email address of the administrative contain for this domain. serial: Serial # of this revision of the requested domain. refresh: Refresh interval (seconds) secondary name servers should use when updating remote copies of this domain. retry: Length of time (seconds) to wait after a failed refresh before making a second attempt. expire: Maximum length of time (seconds) a secondary DNS server should retain remote copies of the zone data without a successful refresh before discarding. minimum-ttl: Minimum length of time (seconds) a client can continue to use a DNS resolution before it should request a new resolution from the server. Can be overridden by individual resource records.  &lt;br /&gt;AAAA ipv6: IPv6 address  &lt;br /&gt;A6(PHP &gt;= 5.1.0) masklen: Length (in bits) to inherit from the target specified by chain. ipv6: Address for this specific record to merge with chain. chain: Parent record to merge with ipv6 data.  &lt;br /&gt;SRV pri: (Priority) lowest priorities should be used first. weight: Ranking to weight which of commonly prioritized targets should be chosen at random. target and port: hostname and port where the requested service can be found. For additional information see: RFC 2782  &lt;br /&gt;NAPTR order and pref: Equivalent to pri and weight above. flags, services, regex, and replacement: Parameters as defined by RFC 2915.  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: Per DNS standards, email addresses are given in user.host format (for example: hostmaster.example.com as opposed to hostmaster@example.com), be sure to check this value and modify if necessary before using it with a functions such as mail(). &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? 1. Using dns_get_record()&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$result = dns_get_record("php.net");&lt;br /&gt;print_r($result);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;Produces output similar to the following: &lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; MX&lt;br /&gt;            [pri] =&gt; 5&lt;br /&gt;            [target] =&gt; pair2.php.net&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 6765&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 64.246.30.37&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 8125&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Since it's very common to want the IP address of a mail server once the MX record has been resolved, dns_get_record() also returns an array in addtl which contains associate records. authns is returned as well containing a list of authoritative name servers. &lt;br /&gt;&lt;br /&gt;????? 2. Using dns_get_record() and DNS_ANY&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;/* Request "ANY" record for php.net, &lt;br /&gt;   and create $authns and $addtl arrays&lt;br /&gt;   containing list of name servers and&lt;br /&gt;   any additional records which go with&lt;br /&gt;   them */&lt;br /&gt;$result = dns_get_record("php.net", DNS_ANY, $authns, $addtl);&lt;br /&gt;echo "Result = ";&lt;br /&gt;print_r($result);&lt;br /&gt;echo "Auth NS = ";&lt;br /&gt;print_r($authns);&lt;br /&gt;echo "Additional = ";&lt;br /&gt;print_r($addtl);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;Produces output similar to the following: &lt;br /&gt;&lt;br /&gt;Result = Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; MX&lt;br /&gt;            [pri] =&gt; 5&lt;br /&gt;            [target] =&gt; pair2.php.net&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 6765&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 64.246.30.37&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 8125&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;br /&gt;Auth NS = Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; NS&lt;br /&gt;            [target] =&gt; remote1.easydns.com&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 10722&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; NS&lt;br /&gt;            [target] =&gt; remote2.easydns.com&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 10722&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [2] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; NS&lt;br /&gt;            [target] =&gt; ns1.easydns.com&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 10722&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [3] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; php.net&lt;br /&gt;            [type] =&gt; NS&lt;br /&gt;            [target] =&gt; ns2.easydns.com&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 10722&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;br /&gt;Additional = Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; pair2.php.net&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 216.92.131.5&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 6766&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; remote1.easydns.com&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 64.39.29.212&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 100384&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [2] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; remote2.easydns.com&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 212.100.224.80&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 81241&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [3] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; ns1.easydns.com&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 216.220.40.243&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 81241&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [4] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [host] =&gt; ns2.easydns.com&lt;br /&gt;            [type] =&gt; A&lt;br /&gt;            [ip] =&gt; 216.220.40.244&lt;br /&gt;            [class] =&gt; IN&lt;br /&gt;            [ttl] =&gt; 81241&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-9133001304116517087?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/9133001304116517087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/dnsgetrecord.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/9133001304116517087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/9133001304116517087'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/dnsgetrecord.html' title='dns_get_record'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4048662563874779951</id><published>2010-03-19T01:06:00.000-07:00</published><updated>2010-03-19T01:07:52.100-07:00</updated><title type='text'>array_map</title><content type='html'>PHP 4 &gt;= 4.0.6, PHP 5)&lt;br /&gt;&lt;br /&gt;array_map --  Applies the callback to the elements of the given arrays &lt;br /&gt;Description&lt;br /&gt;array array_map ( callback callback, array arr1 [, array ...] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;array_map() returns an array containing all the elements of arr1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map() &lt;br /&gt;&lt;br /&gt;????? 1. array_map() example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;function cube($n) &lt;br /&gt;{&lt;br /&gt;    return($n * $n * $n);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$a = array(1, 2, 3, 4, 5);&lt;br /&gt;$b = array_map("cube", $a);&lt;br /&gt;print_r($b);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;This makes $b have: &lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; 1&lt;br /&gt;    [1] =&gt; 8&lt;br /&gt;    [2] =&gt; 27&lt;br /&gt;    [3] =&gt; 64&lt;br /&gt;    [4] =&gt; 125&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? 2. array_map() - using more arrays&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;function show_Spanish($n, $m) &lt;br /&gt;{&lt;br /&gt;    return("The number $n is called $m in Spanish");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function map_Spanish($n, $m) &lt;br /&gt;{&lt;br /&gt;    return(array($n =&gt; $m));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$a = array(1, 2, 3, 4, 5);&lt;br /&gt;$b = array("uno", "dos", "tres", "cuatro", "cinco");&lt;br /&gt;&lt;br /&gt;$c = array_map("show_Spanish", $a, $b);&lt;br /&gt;print_r($c);&lt;br /&gt;&lt;br /&gt;$d = array_map("map_Spanish", $a , $b);&lt;br /&gt;print_r($d);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;// printout of $c&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; The number 1 is called uno in Spanish&lt;br /&gt;    [1] =&gt; The number 2 is called dos in Spanish&lt;br /&gt;    [2] =&gt; The number 3 is called tres in Spanish&lt;br /&gt;    [3] =&gt; The number 4 is called cuatro in Spanish&lt;br /&gt;    [4] =&gt; The number 5 is called cinco in Spanish&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;// printout of $d&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [1] =&gt; uno&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [2] =&gt; dos&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [2] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [3] =&gt; tres&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [3] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [4] =&gt; cuatro&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [4] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [5] =&gt; cinco&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Usually when using two or more arrays, they should be of equal length because the callback function is applied in parallel to the corresponding elements. If the arrays are of unequal length, the shortest one will be extended with empty elements. &lt;br /&gt;&lt;br /&gt;An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function &lt;br /&gt;&lt;br /&gt;????? 3. Creating an array of arrays&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = array(1, 2, 3, 4, 5);&lt;br /&gt;$b = array("one", "two", "three", "four", "five");&lt;br /&gt;$c = array("uno", "dos", "tres", "cuatro", "cinco");&lt;br /&gt;&lt;br /&gt;$d = array_map(null, $a, $b, $c);&lt;br /&gt;print_r($d);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&gt; 1&lt;br /&gt;            [1] =&gt; one&lt;br /&gt;            [2] =&gt; uno&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [1] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&gt; 2&lt;br /&gt;            [1] =&gt; two&lt;br /&gt;            [2] =&gt; dos&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [2] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&gt; 3&lt;br /&gt;            [1] =&gt; three&lt;br /&gt;            [2] =&gt; tres&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [3] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&gt; 4&lt;br /&gt;            [1] =&gt; four&lt;br /&gt;            [2] =&gt; cuatro&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [4] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&gt; 5&lt;br /&gt;            [1] =&gt; five&lt;br /&gt;            [2] =&gt; cinco&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4048662563874779951?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4048662563874779951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/arraymap.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4048662563874779951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4048662563874779951'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/arraymap.html' title='array_map'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6454017981552740633</id><published>2010-03-19T01:02:00.000-07:00</published><updated>2010-03-19T01:05:29.562-07:00</updated><title type='text'>New Functions</title><content type='html'>In PHP 5 there are some new functions. Here is the list of them: &lt;br /&gt;&lt;br /&gt;Arrays:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;array_combine() - Creates an array by using one array for keys and another for its values &lt;br /&gt;&lt;br /&gt;array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function &lt;br /&gt;&lt;br /&gt;array_udiff() - Computes the difference of arrays by using a callback function for data comparison &lt;br /&gt;&lt;br /&gt;array_udiff_assoc() - Computes the difference of arrays with additional index check. The data is compared by using a callback function &lt;br /&gt;&lt;br /&gt;array_udiff_uassoc() - Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also &lt;br /&gt;&lt;br /&gt;array_walk_recursive() - Apply a user function recursively to every member of an array &lt;br /&gt;&lt;br /&gt;array_uintersect_assoc() - Computes the intersection of arrays with additional index check. The data is compared by using a callback function &lt;br /&gt;&lt;br /&gt;array_uintersect_uassoc() - Computes the intersection of arrays with additional index check. Both the data and the indexes are compared by using a callback functions &lt;br /&gt;&lt;br /&gt;array_uintersect() - Computes the intersection of arrays. The data is compared by using a callback function &lt;br /&gt;&lt;br /&gt;InterBase:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ibase_affected_rows() - Return the number of rows that were affected by the previous query &lt;br /&gt;&lt;br /&gt;ibase_backup() - Initiates a backup task in the service manager and returns immediately &lt;br /&gt;&lt;br /&gt;ibase_commit_ret() - Commit a transaction without closing it &lt;br /&gt;&lt;br /&gt;ibase_db_info() - Request statistics about a database &lt;br /&gt;&lt;br /&gt;ibase_drop_db() - Drops a database &lt;br /&gt;&lt;br /&gt;ibase_errcode() - Return an error code &lt;br /&gt;&lt;br /&gt;ibase_free_event_handler() - Cancels a registered event handler &lt;br /&gt;&lt;br /&gt;ibase_gen_id() - Increments the named generator and returns its new value &lt;br /&gt;&lt;br /&gt;ibase_maintain_db() - Execute a maintenance command on the database server &lt;br /&gt;&lt;br /&gt;ibase_name_result() - Assigns a name to a result set &lt;br /&gt;&lt;br /&gt;ibase_num_params() - Return the number of parameters in a prepared query &lt;br /&gt;&lt;br /&gt;ibase_param_info() - Return information about a parameter in a prepared query &lt;br /&gt;&lt;br /&gt;ibase_restore() - Initiates a restore task in the service manager and returns immediately &lt;br /&gt;&lt;br /&gt;ibase_rollback_ret() - Rollback transaction and retain the transaction context &lt;br /&gt;&lt;br /&gt;ibase_server_info() - Request statistics about a database &lt;br /&gt;&lt;br /&gt;ibase_service_attach() - Connect to the service manager &lt;br /&gt;&lt;br /&gt;ibase_service_detach() - Disconnect from the service manager &lt;br /&gt;&lt;br /&gt;ibase_set_event_handler() - Register a callback function to be called when events are posted &lt;br /&gt;&lt;br /&gt;ibase_wait_event() - Wait for an event to be posted by the database &lt;br /&gt;&lt;br /&gt;iconv:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;iconv_mime_decode() - Decodes a MIME header field &lt;br /&gt;&lt;br /&gt;iconv_mime_decode_headers() - Decodes multiple MIME header fields at once &lt;br /&gt;&lt;br /&gt;iconv_mime_encode() - Composes a MIME header field &lt;br /&gt;&lt;br /&gt;iconv_strlen() - Returns the character count of string &lt;br /&gt;&lt;br /&gt;iconv_strpos() - Finds position of first occurrence of a needle within a haystack &lt;br /&gt;&lt;br /&gt;iconv_strrpos() - Finds the last occurrence of a needle within a haystack &lt;br /&gt;&lt;br /&gt;iconv_substr() - Cut out part of a string &lt;br /&gt;&lt;br /&gt;Streams:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;stream_copy_to_stream() - Copies data from one stream to another &lt;br /&gt;&lt;br /&gt;stream_get_line() - Gets line from stream resource up to a given delimiter &lt;br /&gt;&lt;br /&gt;stream_socket_accept() - Accept a connection on a socket created by stream_socket_server() &lt;br /&gt;&lt;br /&gt;stream_socket_client() - Open Internet or Unix domain socket connection &lt;br /&gt;&lt;br /&gt;stream_socket_get_name() - Retrieve the name of the local or remote sockets &lt;br /&gt;&lt;br /&gt;stream_socket_recvfrom() - Receives data from a socket, connected or not &lt;br /&gt;&lt;br /&gt;stream_socket_sendto() - Sends a message to a socket, whether it is connected or not &lt;br /&gt;&lt;br /&gt;stream_socket_server() - Create an Internet or Unix domain server socket &lt;br /&gt;&lt;br /&gt;Date and time related:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;idate() - Format a local time/date as integer &lt;br /&gt;&lt;br /&gt;date_sunset() - Time of sunset for a given day and location &lt;br /&gt;&lt;br /&gt;date_sunrise() - Time of sunrise for a given day and location &lt;br /&gt;&lt;br /&gt;time_nanosleep() - Delay for a number of seconds and nanoseconds &lt;br /&gt;&lt;br /&gt;Strings:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;str_split() - Convert a string to an array &lt;br /&gt;&lt;br /&gt;strpbrk() - Search a string for any of a set of characters &lt;br /&gt;&lt;br /&gt;substr_compare() - Binary safe optionally case insensitive comparison of two strings from an offset, up to length characters &lt;br /&gt;&lt;br /&gt;Other:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;convert_uudecode() - decode a uuencoded string &lt;br /&gt;&lt;br /&gt;convert_uuencode() - uuencode a string &lt;br /&gt;&lt;br /&gt;curl_copy_handle() - Copy a cURL handle along with all of its preferences &lt;br /&gt;&lt;br /&gt;dba_key_split() - Splits a key in string representation into array representation &lt;br /&gt;&lt;br /&gt;dbase_get_header_info() - Get the header info of a dBase database &lt;br /&gt;&lt;br /&gt;dbx_fetch_row() - Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set &lt;br /&gt;&lt;br /&gt;fbsql_set_password() - Change the password for a given user &lt;br /&gt;&lt;br /&gt;file_put_contents() - Write a string to a file &lt;br /&gt;&lt;br /&gt;ftp_alloc() - Allocates space for a file to be uploaded &lt;br /&gt;&lt;br /&gt;get_declared_interfaces() - Returns an array of all declared interfaces &lt;br /&gt;&lt;br /&gt;get_headers() - Fetches all the headers sent by the server in response to a HTTP request &lt;br /&gt;&lt;br /&gt;headers_list() - Returns a list of response headers sent (or ready to send) &lt;br /&gt;&lt;br /&gt;http_build_query() - Generate URL-encoded query string &lt;br /&gt;&lt;br /&gt;image_type_to_extension() - Get file extension for image-type returned by getimagesize(), exif_read_data(), exif_thumbnail(), exif_imagetype() &lt;br /&gt;&lt;br /&gt;imagefilter() - Applies a filter to an image using custom arguments &lt;br /&gt;&lt;br /&gt;imap_getacl() - Gets the ACL for a given mailbox &lt;br /&gt;&lt;br /&gt;ldap_sasl_bind() - Bind to LDAP directory using SASL &lt;br /&gt;&lt;br /&gt;mb_list_encodings() - Returns an array of all supported encodings &lt;br /&gt;&lt;br /&gt;pcntl_getpriority() - Get the priority of any process &lt;br /&gt;&lt;br /&gt;pcntl_wait() - Waits on or returns the status of a forked child as defined by the waitpid() system call &lt;br /&gt;&lt;br /&gt;pg_version() - Returns an array with client, protocol and server version (when available) &lt;br /&gt;&lt;br /&gt;php_check_syntax() - Check the syntax of the specified file &lt;br /&gt;&lt;br /&gt;php_strip_whitespace() - Return source with stripped comments and whitespace &lt;br /&gt;&lt;br /&gt;proc_nice() - Change the priority of the current process &lt;br /&gt;&lt;br /&gt;pspell_config_data_dir() - Change location of language data files &lt;br /&gt;&lt;br /&gt;pspell_config_dict_dir() - Change location of the main word list &lt;br /&gt;&lt;br /&gt;setrawcookie() - Send a cookie without URL-encoding the value &lt;br /&gt;&lt;br /&gt;snmp_read_mib() - Reads and parses a MIB file into the active MIB tree &lt;br /&gt;&lt;br /&gt;sqlite_fetch_column_types() - Return an array of column types from a particular table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6454017981552740633?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6454017981552740633/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/new-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6454017981552740633'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6454017981552740633'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/new-functions.html' title='New Functions'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3016409966166333703</id><published>2010-03-19T01:01:00.000-07:00</published><updated>2010-03-19T01:02:15.677-07:00</updated><title type='text'>URL Functions</title><content type='html'>Introduction&lt;br /&gt;Dealing with URL strings: encoding, decoding and parsing. &lt;br /&gt;&lt;br /&gt;Requirements&lt;br /&gt;No external libraries are needed to build this extension.&lt;br /&gt;&lt;br /&gt;Installation&lt;br /&gt;There is no installation needed to use these functions; they are part of the PHP core.&lt;br /&gt;&lt;br /&gt;Runtime Configuration&lt;br /&gt;This extension has no configuration directives defined in php.ini.&lt;br /&gt;&lt;br /&gt;Resource Types&lt;br /&gt;This extension has no resource types defined.&lt;br /&gt;&lt;br /&gt;Predefined Constants&lt;br /&gt;This extension has no constants defined.&lt;br /&gt;&lt;br /&gt;????&lt;br /&gt;base64_decode -- Decodes data encoded with MIME base64&lt;br /&gt;base64_encode -- Encodes data with MIME base64&lt;br /&gt;get_headers -- Fetches all the headers sent by the server in response to a HTTP request&lt;br /&gt;get_meta_tags -- Extracts all meta tag content attributes from a file and returns an array&lt;br /&gt;http_build_query -- Generate URL-encoded query string&lt;br /&gt;parse_url -- Parse a URL and return its components&lt;br /&gt;rawurldecode -- Decode URL-encoded strings&lt;br /&gt;rawurlencode -- URL-encode according to RFC 1738&lt;br /&gt;urldecode -- Decodes URL-encoded string&lt;br /&gt;urlencode -- URL-encodes string&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3016409966166333703?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3016409966166333703/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/url-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3016409966166333703'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3016409966166333703'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/url-functions.html' title='URL Functions'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8854490583030530582</id><published>2010-03-19T00:53:00.000-07:00</published><updated>2010-03-19T01:01:20.678-07:00</updated><title type='text'>htmlentities</title><content type='html'>htmlentities&lt;br /&gt;(PHP 3, PHP 4, PHP 5)&lt;br /&gt;&lt;br /&gt;htmlentities --  Convert all applicable characters to HTML entities &lt;br /&gt;Description&lt;br /&gt;string htmlentities ( string string [, int quote_style [, string charset]] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities. &lt;br /&gt;&lt;br /&gt;Like htmlspecialchars(), the optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT: &lt;br /&gt;&lt;br /&gt;???? 1. Available quote_style constants&lt;br /&gt;&lt;br /&gt;Constant Name Description &lt;br /&gt;ENT_COMPAT Will convert double-quotes and leave single-quotes alone. &lt;br /&gt;ENT_QUOTES Will convert both double and single quotes. &lt;br /&gt;ENT_NOQUOTES Will leave both double and single quotes unconverted. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Support for the optional quote parameter was added in PHP 4.0.3. &lt;br /&gt;&lt;br /&gt;Like htmlspecialchars(), it takes an optional third argument charset which defines character set used in conversion. Support for this argument was added in PHP 4.1.0. Presently, the ISO-8859-1 character set is used as the default. &lt;br /&gt;&lt;br /&gt;Following character sets are supported in PHP 4.3.0 and later.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8854490583030530582?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8854490583030530582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/htmlentities.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8854490583030530582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8854490583030530582'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/htmlentities.html' title='htmlentities'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-1049760485269680607</id><published>2010-03-15T02:15:00.000-07:00</published><updated>2010-03-15T02:16:34.851-07:00</updated><title type='text'>Forms of vulnerability</title><content type='html'>Incorrectly filtered escape characters&lt;br /&gt;&lt;br /&gt;This form of SQL injection occurs when user input is not filtered for escape characters and is then passed into an SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application.&lt;br /&gt;&lt;br /&gt;The following line of code illustrates this vulnerability:&lt;br /&gt;&lt;br /&gt;statement = "SELECT * FROM users WHERE name = '" + userName + "';"&lt;br /&gt;&lt;br /&gt;This SQL code is designed to pull up the records of the specified username from its table of users. However, if the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as&lt;br /&gt;&lt;br /&gt;a' or 't'='t&lt;br /&gt;&lt;br /&gt;renders this SQL statement by the parent language:&lt;br /&gt;&lt;br /&gt;SELECT * FROM users WHERE name = 'a' OR 't'='t';&lt;br /&gt;&lt;br /&gt;If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of 't'='t' is always true.&lt;br /&gt;&lt;br /&gt;The following value of "userName" in the statement below would cause the deletion of the "users" table as well as the selection of all data from the "userinfo" table (in essence revealing the information of every user), using an API that allows multiple statements:&lt;br /&gt;&lt;br /&gt;a';DROP TABLE users; SELECT * FROM userinfo WHERE 't' = 't&lt;br /&gt;&lt;br /&gt;This input renders the final SQL statement as follows:&lt;br /&gt;&lt;br /&gt;SELECT * FROM users WHERE name = 'a';DROP TABLE users; SELECT * FROM userinfo WHERE 't' = 't';&lt;br /&gt;&lt;br /&gt;While most SQL server implementations allow multiple statements to be executed with one call in this way, some SQL APIs such as PHP's mysql_query() do not allow this for security reasons. This prevents attackers from injecting entirely separate queries, but doesn't stop them from modifying queries.&lt;br /&gt;[edit] Incorrect type handling&lt;br /&gt;&lt;br /&gt;This form of SQL injection occurs when a user supplied field is not strongly typed or is not checked for type constraints. This could take place when a numeric field is to be used in a SQL statement, but the programmer makes no checks to validate that the user supplied input is numeric. For example:&lt;br /&gt;&lt;br /&gt;statement := "SELECT * FROM userinfo WHERE id = " + a_variable + ";"&lt;br /&gt;&lt;br /&gt;It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a string then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to&lt;br /&gt;&lt;br /&gt;1;DROP TABLE users&lt;br /&gt;&lt;br /&gt;will drop (delete) the "users" table from the database, since the SQL would be rendered as follows:&lt;br /&gt;&lt;br /&gt;SELECT * FROM userinfo WHERE id=1;DROP TABLE users;&lt;br /&gt;&lt;br /&gt;[edit] Vulnerabilities inside the database server&lt;br /&gt;&lt;br /&gt;Sometimes vulnerabilities can exist within the database server software itself, as was the case with the MySQL server's mysql_real_escape_string() function[2]. This would allow an attacker to perform a successful SQL injection attack based on bad Unicode characters even if the user's input is being escaped. This bug was patched with the release of version 5.0.22 (released on 24th May 06).&lt;br /&gt;[edit] Blind SQL injection&lt;br /&gt;&lt;br /&gt;Blind SQL Injection is used when a web application is vulnerable to an SQL injection but the results of the injection are not visible to the attacker. The page with the vulnerability may not be one that displays data but will display differently depending on the results of a logical statement injected into the legitimate SQL statement called for that page. This type of attack can become time-intensive because a new statement must be crafted for each bit recovered. There are several tools that can automate these attacks once the location of the vulnerability and the target information has been established.[3]&lt;br /&gt;[edit] Conditional responses&lt;br /&gt;&lt;br /&gt;One type of blind SQL injection forces the database to evaluate a logical statement on an ordinary application screen.&lt;br /&gt;&lt;br /&gt;SELECT booktitle FROM booklist WHERE bookId = 'OOk14cd' AND 1=1;&lt;br /&gt;&lt;br /&gt;will result in a normal page while&lt;br /&gt;&lt;br /&gt;SELECT booktitle FROM booklist WHERE bookId = 'OOk14cd' AND 1=2;&lt;br /&gt;&lt;br /&gt;will likely give a different result if the page is vulnerable to a SQL injection. An injection like this may suggest to the attacker that a blind SQL injection is possible, leaving the attacker to devise statements that evaluate to true or false depending on the contents of another column or table outside of the SELECT statement's column list.[4]&lt;br /&gt;[edit] Conditional errors&lt;br /&gt;&lt;br /&gt;This type of blind SQL injection causes an SQL error by forcing the database to evaluate a statement that causes an error if the WHERE statement is true. For example,&lt;br /&gt;&lt;br /&gt;SELECT 1/0 FROM users WHERE username='Ralph';&lt;br /&gt;&lt;br /&gt;the division by zero will only be evaluated and result in an error if user Ralph exists.&lt;br /&gt;[edit] Time delays&lt;br /&gt;&lt;br /&gt;Time Delays are a type of blind SQL injection that cause the SQL engine to execute a long running query or a time delay statement depending on the logic injected. The attacker can then measure the time the page takes to load to determine if the injected statement is true.&lt;br /&gt;[edit] Preventing SQL injection&lt;br /&gt;&lt;br /&gt;To protect against SQL injection, user input must not directly be embedded in SQL statements. Instead, parameterized statements must be used (preferred), or user input must be carefully escaped or filtered.&lt;br /&gt;[edit] Parameterized statements&lt;br /&gt;&lt;br /&gt;With most development platforms, parameterized statements can be used that work with parameters (sometimes called placeholders or bind variables) instead of embedding user input in the statement. In many cases, the SQL statement is fixed. The user input is then assigned (bound) to a parameter. This is an example using Java and the JDBC API:&lt;br /&gt;&lt;br /&gt;PreparedStatement prep = conn.prepareStatement("SELECT * FROM USERS WHERE USERNAME=? AND PASSWORD=?");&lt;br /&gt;prep.setString(1, username);&lt;br /&gt;prep.setString(2, password);&lt;br /&gt;prep.executeQuery();&lt;br /&gt;&lt;br /&gt;Similarly, in C#:&lt;br /&gt;&lt;br /&gt;using (SqlCommand myCommand = new SqlCommand("SELECT * FROM USERS WHERE USERNAME=@username AND PASSWORD=HASHBYTES('SHA1',&lt;br /&gt; @password)", myConnection))&lt;br /&gt;    {&lt;br /&gt;        myCommand.Parameters.AddWithValue("@username", user);&lt;br /&gt;        myCommand.Parameters.AddWithValue("@password", pass);&lt;br /&gt; &lt;br /&gt;        myConnection.Open();&lt;br /&gt;        SqlDataReader myReader = myCommand.ExecuteReader())&lt;br /&gt;        ...................&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;In PHP version 5 and above, there are multiple choices for using parameterized statements. The PDO[5] database layer is one of them:&lt;br /&gt;&lt;br /&gt;$db = new PDO('pgsql:dbname=database');&lt;br /&gt;$stmt = $db-&gt;prepare("SELECT priv FROM testUsers WHERE username=:username AND password=:password");&lt;br /&gt;$stmt-&gt;bindParam(':username', $user);&lt;br /&gt;$stmt-&gt;bindParam(':password', $pass);&lt;br /&gt;$stmt-&gt;execute();&lt;br /&gt;&lt;br /&gt;There are also vendor-specific methods; for instance, using the mysqli[6] extension for MySQL 4.1 and above to create parameterized statements[7]:&lt;br /&gt;&lt;br /&gt;$db = new mysqli("localhost", "user", "pass", "database");&lt;br /&gt;$stmt = $db -&gt; prepare("SELECT priv FROM testUsers WHERE username=? AND password=?");&lt;br /&gt;$stmt -&gt; bind_param("ss", $user, $pass);&lt;br /&gt;$stmt -&gt; execute();&lt;br /&gt;&lt;br /&gt;In ColdFusion, the CFQUERYPARAM statement is useful in conjunction with the CFQUERY statement to nullify the effect of SQL code passed within the CFQUERYPARAM value as part of the SQL clause.[8][9]. An example is below.&lt;br /&gt;&lt;br /&gt;&lt;cfquery name="Recordset1" datasource="cafetownsend"&gt;&lt;br /&gt;SELECT *&lt;br /&gt;FROM COMMENTS&lt;br /&gt;WHERE COMMENT_ID =&lt;cfqueryparam value="#URL.COMMENT_ID#" cfsqltype="cf_sql_numeric"&gt;&lt;br /&gt;&lt;/cfquery&gt;&lt;br /&gt;&lt;br /&gt;[edit] Enforcement at the database level&lt;br /&gt;&lt;br /&gt;Currently only the H2 Database Engine supports the ability to enforce query parameterization.[10] However, one drawback is that query-by-example may not be possible or practical because it's difficult to implement query-by-example using parametrized queries.&lt;br /&gt;[edit] Enforcement at the coding level&lt;br /&gt;&lt;br /&gt;Using object-relational mapping libraries avoids the need to write SQL code. The ORM library in effect will generate parameterized SQL statements from object-oriented code.&lt;br /&gt;[edit] Escaping&lt;br /&gt;&lt;br /&gt;A straight-forward, though error-prone, way to prevent injections is to escape dangerous characters. One of the reasons for it being error prone is that it is a type of blacklist which is less robust than a whitelist. For instance, every occurrence of a single quote (') in a parameter must be replaced by two single quotes ('') to form a valid SQL string literal. In PHP, for example, it is usual to escape parameters using the function mysql_real_escape_string before sending the SQL query:&lt;br /&gt;&lt;br /&gt;$query = sprintf("SELECT * FROM Users where UserName='%s' and Password='%s'",&lt;br /&gt;                  mysql_real_escape_string($Username),&lt;br /&gt;                  mysql_real_escape_string($Password));&lt;br /&gt;mysql_query($query);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-1049760485269680607?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/1049760485269680607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/forms-of-vulnerability.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1049760485269680607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1049760485269680607'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/forms-of-vulnerability.html' title='Forms of vulnerability'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4155703122627396881</id><published>2010-03-15T02:14:00.000-07:00</published><updated>2010-03-15T02:15:29.411-07:00</updated><title type='text'>How to check for SQL injection vulnerabilities</title><content type='html'>Securing your website and web applications from SQL Injection involves a three-part process:&lt;br /&gt;&lt;br /&gt;   1. Analysing the present state of security present by performing a thorough audit of your website and web applications for SQL Injection and other hacking vulnerabilities.&lt;br /&gt;   2. Making sure that you use coding best practice santising your web applications and all other components of your IT infrastructure.&lt;br /&gt;   3. Regularly performing a web security audit after each change and addition to your web components.&lt;br /&gt;&lt;br /&gt;Furthermore, the principles you need to keep in mind when checking for SQL Injection and all other hacking techniques are the following: “Which parts of a website we thought are secure are open to hack attacks?” and “what data can we throw at an application to cause it to perform something it shouldn’t do?”.&lt;br /&gt;&lt;br /&gt;Checking for SQL Injection vulnerabilities involves auditing your website and web applications. Manual vulnerability auditing is complex and very time-consuming. It also demands a high-level of expertise and the ability to keep track of considerable volumes of code and of all the latest tricks of the hacker’s ‘trade’.&lt;br /&gt;&lt;br /&gt;The best way to check whether your web site and applications are vulnerable to SQL injection attacks is by using an automated and heuristic web vulnerability scanner.&lt;br /&gt;&lt;br /&gt;An automated web vulnerability scanner crawls your entire website and should automatically check for vulnerabilities to SQL Injection attacks. It will indicate which URLs/scripts are vulnerable to SQL injection so that you can immediately fix the code. Besides SQL injection vulnerabilities a web application scanner will also check for Cross site scripting and other web vulnerabilities.&lt;br /&gt;&lt;br /&gt;Signature-Matching versus Heuristic Scanning for SQL Injection&lt;br /&gt;Whereas many organisations understand the need for automating and regularising web auditing, few appreciate the necessity of scanning both off-the-shelf AND bespoke web applications. The general misconception is these custom web applications are not vulnerable to hacking attacks. This arises more out of the “it can never happen to me” phenomenon and the confidence website owners place in their developers.&lt;br /&gt;&lt;br /&gt;A search on Google News returned 240 matches on the keyword “SQL Injection” (at time of writing). Secunia and SecuObs report dozens of vulnerabilities of known web applications on a daily basis. Yet, examples of hacked custom applications are rarely cited in the media. This is because it is only the known organisations (e.g. Choicepoint, AT&amp;T, PayPal) that hit the headlines over the past few months.&lt;br /&gt;&lt;br /&gt;It is critical to understand that custom web applications are probably the most vulnerable and definitely attract the greatest number of hackers simply because they know that such applications do not pass through the rigorous testing and quality assurance processes of off-the-shelf ones.&lt;br /&gt;&lt;br /&gt;This means that scanning a custom web application with only a signature-based scanner will not pinpoint vulnerabilities to SQL Injection and any other hacking techniques.&lt;br /&gt;&lt;br /&gt;Establishing and testing against a database of signatures of vulnerabilities for known applications is not enough. This is passive auditing because it will only cover off-the-shelf applications and any vulnerabilities to new hacking techniques will not be discovered. In addition, signature matching would do little when a hacker launches an SQL Injection attack on your custom web applications. Hack attacks are not based on signature file testing – hackers understand that known applications, systems and servers are being updated and secured constantly and consistently by respective vendors. It is custom applications that are the proverbial honey pot.&lt;br /&gt;&lt;br /&gt;It is only a handful of products that deploy rigorous and heuristic technologies to identify the real threats. True automated web vulnerability scanning almost entirely depends on (a) how well your site is crawled to establish its structure and various components and links, and (b) on the ability of the scanner to leverage intelligently the various hacking methods and techniques against your web applications.&lt;br /&gt;&lt;br /&gt;It would be useless to detect the known vulnerabilities of known applications alone. A significant degree of heuristics is involved in detecting vulnerabilities since hackers are extremely creative and launch their attacks against bespoke web applications to create maximum impact.&lt;br /&gt;&lt;br /&gt;How can Acunetix help you in auditing your site for SQL Injection?&lt;br /&gt;Acunetix was founded to combat the alarming rise in web attacks including SQL Injection and Cross-Site Scripting among others. Take a product tour to find out how Acunetix Web Vulnerability Scanner can help you or download the scanner today!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4155703122627396881?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4155703122627396881/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/how-to-check-for-sql-injection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4155703122627396881'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4155703122627396881'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/how-to-check-for-sql-injection.html' title='How to check for SQL injection vulnerabilities'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5974620178876977355</id><published>2010-03-15T02:10:00.000-07:00</published><updated>2010-03-15T02:14:33.296-07:00</updated><title type='text'>SQL Injection: What is it?</title><content type='html'>SQL Injection is one of the many web attack mechanisms used by hackers to steal data from organizations. It is perhaps one of the most common application layer attack techniques used today. It is the type of attack that takes advantage of improper coding of your web applications that allows hacker to inject SQL commands into say a login form to allow them to gain access to the data held within your database.&lt;br /&gt;&lt;br /&gt;In essence, SQL Injection arises because the fields available for user input allow SQL statements to pass through and query the database directly.&lt;br /&gt;&lt;br /&gt;SQL Injection: An In-depth Explanation&lt;br /&gt;Web applications allow legitimate website visitors to submit and retrieve data to/from a database over the Internet using their preferred web browser. Databases are central to modern websites – they store data needed for websites to deliver specific content to visitors and render information to customers, suppliers, employees and a host of stakeholders. User credentials, financial and payment information, company statistics may all be resident within a database and accessed by legitimate users through off-the-shelf and custom web applications. Web applications and databases allow you to regularly run your business.&lt;br /&gt;&lt;br /&gt;SQL Injection is the hacking technique which attempts to pass SQL commands (statements) through a web application for execution by the backend database. If not sanitized properly, web applications may result in SQL Injection attacks that allow hackers to view information from the database and/or even wipe it out.&lt;br /&gt;&lt;br /&gt;Such features as login pages, support and product request forms, feedback forms, search pages, shopping carts and the general delivery of dynamic content, shape modern websites and provide businesses with the means necessary to communicate with prospects and customers. These website features are all examples of web applications which may be either purchased off-the-shelf or developed as bespoke programs.&lt;br /&gt;&lt;br /&gt;These website features are all susceptible to SQL Injection attacks which arise because the fields available for user input allow SQL statements to pass through and query the database directly.&lt;br /&gt;&lt;br /&gt;SQL Injection: A Simple Example&lt;br /&gt;Take a simple login page where a legitimate user would enter his username and password combination to enter a secure area to view his personal details or upload his comments in a forum.&lt;br /&gt;&lt;br /&gt;When the legitimate user submits his details, an SQL query is generated from these details and submitted to the database for verification. If valid, the user is allowed access. In other words, the web application that controls the login page will communicate with the database through a series of planned commands so as to verify the username and password combination. On verification, the legitimate user is granted appropriate access.&lt;br /&gt;&lt;br /&gt;Through SQL Injection, the hacker may input specifically crafted SQL commands with the intent of bypassing the login form barrier and seeing what lies behind it. This is only possible if the inputs are not properly sanitised (i.e., made invulnerable) and sent directly with the SQL query to the database. SQL Injection vulnerabilities provide the means for a hacker to communicate directly to the database.&lt;br /&gt;&lt;br /&gt;The technologies vulnerable to this attack are dynamic script languages including ASP, ASP.NET, PHP, JSP, and CGI. All an attacker needs to perform an SQL Injection hacking attack is a web browser, knowledge of SQL queries and creative guess work to important table and field names. The sheer simplicity of SQL Injection has fuelled its popularity.&lt;br /&gt;&lt;br /&gt;Other contents:&lt;br /&gt;Why is it possible to pass SQL Queries to the database even though this is hidden behind a firewall?&lt;br /&gt;Is my database at risk to SQL Injection?&lt;br /&gt;What is the impact of SQL Injection?&lt;br /&gt;Example of a SQL Injection Attack&lt;br /&gt;How do I prevent SQL Injection attacks?&lt;br /&gt;&lt;br /&gt;Why is it possible to pass SQL queries directly to a database that is hidden behind a firewall and any other security mechanism?&lt;br /&gt;Firewalls and similar intrusion detection mechanisms provide little or no defense against full-scale SQL Injection web attacks.&lt;br /&gt;&lt;br /&gt;Since your website needs to be public, security mechanisms will allow public web traffic to communicate with your web application/s (generally over port 80/443). The web application has open access to the database in order to return (update) the requested (changed) information.&lt;br /&gt;&lt;br /&gt;In SQL Injection, the hacker uses SQL queries and creativity to get to the database of sensitive corporate data through the web application.&lt;br /&gt;&lt;br /&gt;SQL or Structured Query Language is the computer language that allows you to store, manipulate, and retrieve data stored in a relational database (or a collection of tables which organise and structure data). SQL is, in fact, the only way that a web application (and users) can interact with the database. Examples of relational databases include Oracle, Microsoft Access, MS SQL Server, MySQL, and Filemaker Pro, all of which use SQL as their basic building blocks.&lt;br /&gt;&lt;br /&gt;SQL commands include SELECT, INSERT, DELETE and DROP TABLE. DROP TABLE is as ominous as it sounds and in fact will eliminate the table with a particular name.&lt;br /&gt;&lt;br /&gt;In the legitimate scenario of the login page example above, the SQL commands planned for the web application may look like the following:&lt;br /&gt;&lt;br /&gt;SELECT count(*)&lt;br /&gt;FROM users_list_table&lt;br /&gt;WHERE username=’FIELD_USERNAME’&lt;br /&gt;AND password=’FIELD_PASSWORD”&lt;br /&gt;&lt;br /&gt;In plain English, this SQL command (from the web application) instructs the database to match the username and password input by the legitimate user to the combination it has already stored.&lt;br /&gt;&lt;br /&gt;Each type of web application is hard coded with specific SQL queries that it will execute when performing its legitimate functions and communicating with the database. If any input field of the web application is not properly sanitised, a hacker may inject additional SQL commands that broaden the range of SQL commands the web application will execute, thus going beyond the original intended design and function.&lt;br /&gt;&lt;br /&gt;A hacker will thus have a clear channel of communication (or, in layman terms, a tunnel) to the database irrespective of all the intrusion detection systems and network security equipment installed before the physical database server.&lt;br /&gt;&lt;br /&gt;Is my database at risk to SQL Injection?&lt;br /&gt;SQL Injection is one of the most common application layer attacks currently being used on the Internet. Despite the fact that it is relatively easy to protect against SQL Injection, there are a large number of web applications that remain vulnerable.&lt;br /&gt;&lt;br /&gt;According to the Web Application Security Consortium (WASC) 9% of the total hacking incidents reported in the media until 27th July 2006 were due to SQL Injection. More recent data from our own research shows that about 50% of the websites we have scanned this year are susceptible to SQL Injection vulnerabilities.&lt;br /&gt;&lt;br /&gt;It may be difficult to answer the question whether your web site and web applications are vulnerable to SQL Injection especially if you are not a programmer or you are not the person who has coded your web applications.&lt;br /&gt;&lt;br /&gt;Our experience leads us to believe that there is a significant chance that your data is already at risk from SQL Injection.&lt;br /&gt;&lt;br /&gt;Whether an attacker is able to see the data stored on the database or not, really depends on how your website is coded to display the results of the queries sent. What is certain is that the attacker will be able to execute arbitrary SQL Commands on the vulnerable system, either to compromise it or else to obtain information.&lt;br /&gt;&lt;br /&gt;If improperly coded, then you run the risk of having your customer and company data compromised.&lt;br /&gt;&lt;br /&gt;What an attacker gains access to also depends on the level of security set by the database. The database could be set to restrict to certain commands only. A read access normally is enabled for use by web application back ends.&lt;br /&gt;&lt;br /&gt;Even if an attacker is not able to modify the system, he would still be able to read valuable information.&lt;br /&gt;&lt;br /&gt;What is the impact of SQL Injection?&lt;br /&gt;Once an attacker realizes that a system is vulnerable to SQL Injection, he is able to inject SQL Query / Commands through an input form field. This is equivalent to handing the attacker your database and allowing him to execute any SQL command including DROP TABLE to the database!&lt;br /&gt;&lt;br /&gt;An attacker may execute arbitrary SQL statements on the vulnerable system. This may compromise the integrity of your database and/or expose sensitive information. Depending on the back-end database in use, SQL injection vulnerabilities lead to varying levels of data/system access for the attacker. It may be possible to manipulate existing queries, to UNION (used to select related information from two tables) arbitrary data, use subselects, or append additional queries.&lt;br /&gt;&lt;br /&gt;In some cases, it may be possible to read in or write out to files, or to execute shell commands on the underlying operating system. Certain SQL Servers such as Microsoft SQL Server contain stored and extended procedures (database server functions). If an attacker can obtain access to these procedures, it could spell disaster.&lt;br /&gt;&lt;br /&gt;Unfortunately the impact of SQL Injection is only uncovered when the theft is discovered. Data is being unwittingly stolen through various hack attacks all the time. The more expert of hackers rarely get caught.&lt;br /&gt;&lt;br /&gt;Example of a SQLInjection Attack&lt;br /&gt;Here is a sample basic HTML form with two inputs, login and password.&lt;br /&gt;&lt;br /&gt;&lt;form method="post" action="http://testasp.acunetix.com/login.asp"&gt;&lt;br /&gt;&lt;input name="tfUName" type="text" id="tfUName"&gt;&lt;br /&gt;&lt;input name="tfUPass" type="password" id="tfUPass"&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;The easiest way for the login.asp to work is by building a database query that looks like this:&lt;br /&gt;&lt;br /&gt;SELECT id&lt;br /&gt;FROM logins&lt;br /&gt;WHERE username = '$username'&lt;br /&gt;AND password = '$password’&lt;br /&gt;&lt;br /&gt;If the variables $username and $password are requested directly from the user's input, this can easily be compromised. Suppose that we gave "Joe" as a username and that the following string was provided as a password: anything' OR 'x'='x&lt;br /&gt;&lt;br /&gt;SELECT id&lt;br /&gt;FROM logins&lt;br /&gt;WHERE username = 'Joe'&lt;br /&gt;AND password = 'anything' OR 'x'='x'&lt;br /&gt;&lt;br /&gt;As the inputs of the web application are not properly sanitised, the use of the single quotes has turned the WHERE SQL command into a two-component clause.&lt;br /&gt;&lt;br /&gt;The 'x'='x' part guarantees to be true regardless of what the first part contains.&lt;br /&gt;&lt;br /&gt;This will allow the attacker to bypass the login form without actually knowing a valid username / password combination!&lt;br /&gt;&lt;br /&gt;How do I prevent SQL Injection attacks?&lt;br /&gt;Firewalls and similar intrusion detection mechanisms provide little defense against full-scale web attacks. Since your website needs to be public, security mechanisms will allow public web traffic to communicate with your databases servers through web applications. Isn’t this what they have been designed to do?&lt;br /&gt;&lt;br /&gt;Patching your servers, databases, programming languages and operating systems is critical but will in no way the best way to prevent SQL Injection Attacks.&lt;br /&gt;&lt;br /&gt;Read the full article on How to Check for and Prevent SQL Injection Attacks.&lt;br /&gt;&lt;br /&gt;Download Acunetix Web Vulnerability Scanner and find out if your site is vulnerable!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5974620178876977355?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5974620178876977355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/sql-injection-what-is-it.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5974620178876977355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5974620178876977355'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/03/sql-injection-what-is-it.html' title='SQL Injection: What is it?'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-1704869572489213701</id><published>2010-02-20T22:00:00.001-08:00</published><updated>2010-02-20T22:00:47.488-08:00</updated><title type='text'>substr_compare</title><content type='html'>int substr_compare ( string main_str, string str, int offset [, int length [, bool case_insensitivity]] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;substr_compare() compares main_str from position offset with str up to length characters. &lt;br /&gt;&lt;br /&gt;Returns &lt; 0 if main_str from position offset is less than str, &gt; 0 if it is greater than str, and 0 if they are equal. If length is equal or greater than length of main_str and length is set, substr_compare() prints warning and returns FALSE. &lt;br /&gt;&lt;br /&gt;If case_insensitivity is TRUE, comparison is case insensitive. &lt;br /&gt;&lt;br /&gt;????? 1. A substr_compare() example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo substr_compare("abcde", "bc", 1, 2); // 0&lt;br /&gt;echo substr_compare("abcde", "bcg", 1, 2); // 0&lt;br /&gt;echo substr_compare("abcde", "BC", 1, 2, true); // 0&lt;br /&gt;echo substr_compare("abcde", "bc", 1, 3); // 1&lt;br /&gt;echo substr_compare("abcde", "cd", 1, 2); // -1&lt;br /&gt;echo substr_compare("abcde", "abc", 5, 1); // warning&lt;br /&gt;?&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-1704869572489213701?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/1704869572489213701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/substrcompare.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1704869572489213701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1704869572489213701'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/substrcompare.html' title='substr_compare'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8735310434385504470</id><published>2010-02-20T21:59:00.000-08:00</published><updated>2010-02-20T22:00:00.720-08:00</updated><title type='text'>PHP str_word_count</title><content type='html'>mixed str_word_count ( string string [, int format [, string charlist]] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Counts the number of words inside string. If the optional format is not specified, then the return value will be an integer representing the number of words found. In the event the format is specified, the return value will be an array, content of which is dependent on the format. The possible value for the format and the resultant outputs are listed below. &lt;br /&gt;&lt;br /&gt;For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters. &lt;br /&gt;&lt;br /&gt;Parameters&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;string&lt;br /&gt;The string &lt;br /&gt;&lt;br /&gt;format&lt;br /&gt;Specify the return value of this function. The current supported values are: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0 - returns the number of words found &lt;br /&gt;&lt;br /&gt;1 - returns an array containing all the words found inside the string &lt;br /&gt;&lt;br /&gt;2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;charlist&lt;br /&gt;A list of additional characters which will be considered as 'word' &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Return Values&lt;br /&gt;Returns an array or an integer, depending on the format chosen. &lt;br /&gt;&lt;br /&gt;ChangeLog&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Version Description &lt;br /&gt;5.1.0 Added the charlist parameter  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Examples&lt;br /&gt;????? 1. A str_word_count() example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$str = "Hello fri3nd, you're&lt;br /&gt;       looking          good today!";&lt;br /&gt;&lt;br /&gt;print_r(str_word_count($str, 1));&lt;br /&gt;print_r(str_word_count($str, 2));&lt;br /&gt;print_r(str_word_count($str, 1, 'àáãç3'));&lt;br /&gt;&lt;br /&gt;echo str_word_count($str);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Hello&lt;br /&gt;    [1] =&gt; fri&lt;br /&gt;    [2] =&gt; nd&lt;br /&gt;    [3] =&gt; you're&lt;br /&gt;    [4] =&gt; looking&lt;br /&gt;    [5] =&gt; good&lt;br /&gt;    [6] =&gt; today&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Hello&lt;br /&gt;    [6] =&gt; fri&lt;br /&gt;    [10] =&gt; nd&lt;br /&gt;    [14] =&gt; you're&lt;br /&gt;    [29] =&gt; looking&lt;br /&gt;    [46] =&gt; good&lt;br /&gt;    [51] =&gt; today&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Hello&lt;br /&gt;    [1] =&gt; fri3nd&lt;br /&gt;    [2] =&gt; you're&lt;br /&gt;    [3] =&gt; looking&lt;br /&gt;    [4] =&gt; good&lt;br /&gt;    [5] =&gt; today&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;7&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8735310434385504470?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8735310434385504470/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-strwordcount.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8735310434385504470'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8735310434385504470'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-strwordcount.html' title='PHP str_word_count'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8600378255371068641</id><published>2010-02-20T21:58:00.001-08:00</published><updated>2010-02-20T21:58:59.638-08:00</updated><title type='text'>PHP mb_detect_encoding</title><content type='html'>Description&lt;br /&gt;string mb_detect_encoding ( string str [, mixed encoding_list [, bool strict]] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mb_detect_encoding() detects character encoding in string str. It returns detected character encoding. &lt;br /&gt;&lt;br /&gt;encoding_list is list of character encoding. Encoding order may be specified by array or comma separated list string. &lt;br /&gt;&lt;br /&gt;If encoding_list is omitted, detect_order is used. &lt;br /&gt;&lt;br /&gt;????? 1. mb_detect_encoding() example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;/* Detect character encoding with current detect_order */&lt;br /&gt;echo mb_detect_encoding($str);&lt;br /&gt;&lt;br /&gt;/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */&lt;br /&gt;echo mb_detect_encoding($str, "auto");&lt;br /&gt;&lt;br /&gt;/* Specify encoding_list character encoding by comma separated list */&lt;br /&gt;echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");&lt;br /&gt;&lt;br /&gt;/* Use array to specify encoding_list  */&lt;br /&gt;$ary[] = "ASCII";&lt;br /&gt;$ary[] = "JIS";&lt;br /&gt;$ary[] = "EUC-JP";&lt;br /&gt;echo mb_detect_encoding($str, $ary);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8600378255371068641?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8600378255371068641/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-mbdetectencoding.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8600378255371068641'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8600378255371068641'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-mbdetectencoding.html' title='PHP mb_detect_encoding'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-931052568946127709</id><published>2010-02-20T21:57:00.000-08:00</published><updated>2010-02-20T21:58:13.539-08:00</updated><title type='text'>PHP strings</title><content type='html'>PHP Manual &lt;br /&gt;????? ??? 11. Types ????? &lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Strings&lt;br /&gt;A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode. See utf8_encode() and utf8_decode() for some Unicode support. &lt;br /&gt;&lt;br /&gt;????: It is no problem for a string to become very large. There is no practical bound to the size of strings imposed by PHP, so there is no reason at all to worry about long strings. &lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;A string literal can be specified in three different ways. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;single quoted &lt;br /&gt;&lt;br /&gt;double quoted &lt;br /&gt;&lt;br /&gt;heredoc syntax &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Single quoted&lt;br /&gt;The easiest way to specify a simple string is to enclose it in single quotes (the character '). &lt;br /&gt;&lt;br /&gt;To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself. &lt;br /&gt;&lt;br /&gt;????: In PHP 3, a warning will be issued at the E_NOTICE level when this happens. &lt;br /&gt;&lt;br /&gt;????: Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo 'this is a simple string';&lt;br /&gt;&lt;br /&gt;echo 'You can also have embedded newlines in &lt;br /&gt;strings this way as it is&lt;br /&gt;okay to do';&lt;br /&gt;&lt;br /&gt;// Outputs: Arnold once said: "I'll be back"&lt;br /&gt;echo 'Arnold once said: "I\'ll be back"';&lt;br /&gt;&lt;br /&gt;// Outputs: You deleted C:\*.*?&lt;br /&gt;echo 'You deleted C:\\*.*?';&lt;br /&gt;&lt;br /&gt;// Outputs: You deleted C:\*.*?&lt;br /&gt;echo 'You deleted C:\*.*?';&lt;br /&gt;&lt;br /&gt;// Outputs: This will not expand: \n a newline&lt;br /&gt;echo 'This will not expand: \n a newline';&lt;br /&gt;&lt;br /&gt;// Outputs: Variables do not $expand $either&lt;br /&gt;echo 'Variables do not $expand $either';&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Double quoted&lt;br /&gt;If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters: &lt;br /&gt;&lt;br /&gt;???? 11-1. Escaped characters&lt;br /&gt;&lt;br /&gt;sequence meaning &lt;br /&gt;\n linefeed (LF or 0x0A (10) in ASCII) &lt;br /&gt;\r carriage return (CR or 0x0D (13) in ASCII) &lt;br /&gt;\t horizontal tab (HT or 0x09 (9) in ASCII) &lt;br /&gt;\\ backslash &lt;br /&gt;\$ dollar sign &lt;br /&gt;\" double-quote &lt;br /&gt;\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation  &lt;br /&gt;\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation  &lt;br /&gt;&lt;br /&gt;Again, if you try to escape any other character, the backslash will be printed too! Before PHP 5.1.1, backslash in \{$var} hasn't been printed. &lt;br /&gt;&lt;br /&gt;But the most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details. &lt;br /&gt;&lt;br /&gt;Heredoc&lt;br /&gt;Another way to delimit strings is by using heredoc syntax ("&lt;&lt;&lt;"). One should provide an identifier after &lt;&lt;&lt;, then the string, and then the same identifier to close the quotation. &lt;br /&gt;&lt;br /&gt;The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example. Closing delimiter (possibly followed by a semicolon) must be followed by a newline too. &lt;br /&gt;&lt;br /&gt;If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script. &lt;br /&gt;&lt;br /&gt;It is not allowed to use heredoc syntax in initializing class members. Use other string syntaxes instead. ????? 11-3. Invalid example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;class foo {&lt;br /&gt;    public $bar = &lt;&lt;&lt;EOT&lt;br /&gt;bar&lt;br /&gt;EOT;&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings. ????? 11-4. Heredoc string quoting example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$str = &lt;&lt;&lt;EOD&lt;br /&gt;Example of string&lt;br /&gt;spanning multiple lines&lt;br /&gt;using heredoc syntax.&lt;br /&gt;EOD;&lt;br /&gt;&lt;br /&gt;/* More complex example, with variables. */&lt;br /&gt;class foo&lt;br /&gt;{&lt;br /&gt;    var $foo;&lt;br /&gt;    var $bar;&lt;br /&gt;&lt;br /&gt;    function foo()&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;foo = 'Foo';&lt;br /&gt;        $this-&gt;bar = array('Bar1', 'Bar2', 'Bar3');&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$foo = new foo();&lt;br /&gt;$name = 'MyName';&lt;br /&gt;&lt;br /&gt;echo &lt;&lt;&lt;EOT&lt;br /&gt;My name is "$name". I am printing some $foo-&gt;foo.&lt;br /&gt;Now, I am printing some {$foo-&gt;bar[1]}.&lt;br /&gt;This should print a capital 'A': \x41&lt;br /&gt;EOT;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: Heredoc support was added in PHP 4. &lt;br /&gt;&lt;br /&gt;Variable parsing&lt;br /&gt;When a string is specified in double quotes or with heredoc, variables are parsed within it. &lt;br /&gt;&lt;br /&gt;There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to parse a variable, an array value, or an object property. &lt;br /&gt;&lt;br /&gt;The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. &lt;br /&gt;&lt;br /&gt;Simple syntax&lt;br /&gt;If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces if you want to explicitly specify the end of the name. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$beer = 'Heineken';&lt;br /&gt;echo "$beer's taste is great"; // works, "'" is an invalid character for varnames&lt;br /&gt;echo "He drank some $beers";   // won't work, 's' is a valid character for varnames&lt;br /&gt;echo "He drank some ${beer}s"; // works&lt;br /&gt;echo "He drank some {$beer}s"; // works&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Similarly, you can also have an array index or an object property parsed. With array indices, the closing square bracket (]) marks the end of the index. For object properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// These examples are specific to using arrays inside of strings.&lt;br /&gt;// When outside of a string, always quote your array string keys &lt;br /&gt;// and do not use {braces} when outside of strings either.&lt;br /&gt;&lt;br /&gt;// Let's show all errors&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;&lt;br /&gt;$fruits = array('strawberry' =&gt; 'red', 'banana' =&gt; 'yellow');&lt;br /&gt;&lt;br /&gt;// Works but note that this works differently outside string-quotes&lt;br /&gt;echo "A banana is $fruits[banana].";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "A banana is {$fruits['banana']}.";&lt;br /&gt;&lt;br /&gt;// Works but PHP looks for a constant named banana first&lt;br /&gt;// as described below.&lt;br /&gt;echo "A banana is {$fruits[banana]}.";&lt;br /&gt;&lt;br /&gt;// Won't work, use braces.  This results in a parse error.&lt;br /&gt;echo "A banana is $fruits['banana'].";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "A banana is " . $fruits['banana'] . ".";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This square is $square-&gt;width meters broad.";&lt;br /&gt;&lt;br /&gt;// Won't work. For a solution, see the complex syntax.&lt;br /&gt;echo "This square is $square-&gt;width00 centimeters broad.";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For anything more complex, you should use the complex syntax. &lt;br /&gt;&lt;br /&gt;Complex (curly) syntax&lt;br /&gt;This isn't called complex because the syntax is complex, but because you can include complex expressions this way. &lt;br /&gt;&lt;br /&gt;In fact, you can include any value that is in the namespace in strings with this syntax. You simply write the expression the same way as you would outside the string, and then include it in { and }. Since you can't escape '{', this syntax will only be recognised when the $ is immediately following the {. (Use "{\$" to get a literal "{$"). Some examples to make it clear: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Let's show all errors&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;&lt;br /&gt;$great = 'fantastic';&lt;br /&gt;&lt;br /&gt;// Won't work, outputs: This is { fantastic}&lt;br /&gt;echo "This is { $great}";&lt;br /&gt;&lt;br /&gt;// Works, outputs: This is fantastic&lt;br /&gt;echo "This is {$great}";&lt;br /&gt;echo "This is ${great}";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This square is {$square-&gt;width}00 centimeters broad."; &lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This works: {$arr[4][3]}";&lt;br /&gt;&lt;br /&gt;// This is wrong for the same reason as $foo[bar] is wrong &lt;br /&gt;// outside a string.  In other words, it will still work but&lt;br /&gt;// because PHP first looks for a constant named foo, it will&lt;br /&gt;// throw an error of level E_NOTICE (undefined constant).&lt;br /&gt;echo "This is wrong: {$arr[foo][3]}"; &lt;br /&gt;&lt;br /&gt;// Works.  When using multi-dimensional arrays, always use&lt;br /&gt;// braces around arrays when inside of strings&lt;br /&gt;echo "This works: {$arr['foo'][3]}";&lt;br /&gt;&lt;br /&gt;// Works.&lt;br /&gt;echo "This works: " . $arr['foo'][3];&lt;br /&gt;&lt;br /&gt;echo "You can even write {$obj-&gt;values[3]-&gt;name}";&lt;br /&gt;&lt;br /&gt;echo "This is the value of the var named $name: {${$name}}";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;String access and modification by character&lt;br /&gt;Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array-brackets like $str[42] so think of a string as an array of characters. &lt;br /&gt;&lt;br /&gt;????: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred. &lt;br /&gt;&lt;br /&gt;????? 11-5. Some string examples&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Get the first character of a string&lt;br /&gt;$str = 'This is a test.';&lt;br /&gt;$first = $str[0];&lt;br /&gt;&lt;br /&gt;// Get the third character of a string&lt;br /&gt;$third = $str[2];&lt;br /&gt;&lt;br /&gt;// Get the last character of a string.&lt;br /&gt;$str = 'This is still a test.';&lt;br /&gt;$last = $str[strlen($str)-1]; &lt;br /&gt;&lt;br /&gt;// Modify the last character of a string&lt;br /&gt;$str = 'Look at the sea';&lt;br /&gt;$str[strlen($str)-1] = 'e';&lt;br /&gt;&lt;br /&gt;// Alternative method using {}&lt;br /&gt;$third = $str{2};&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Useful functions and operators&lt;br /&gt;Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this. Please see String operators for more information. &lt;br /&gt;&lt;br /&gt;There are a lot of useful functions for string modification. &lt;br /&gt;&lt;br /&gt;See the string functions section for general functions, the regular expression functions for advanced find&amp;replacing (in two tastes: Perl and POSIX extended). &lt;br /&gt;&lt;br /&gt;There are also functions for URL-strings, and functions to encrypt/decrypt strings (mcrypt and mhash). &lt;br /&gt;&lt;br /&gt;Finally, if you still didn't find what you're looking for, see also the character type functions. &lt;br /&gt;&lt;br /&gt;Converting to string&lt;br /&gt;You can convert a value to a string using the (string) cast, or the strval() function. String conversion is automatically done in the scope of an expression for you where a string is needed. This happens when you use the echo() or print() functions, or when you compare a variable value to a string. Reading the manual sections on Types and Type Juggling will make the following clearer. See also settype(). &lt;br /&gt;&lt;br /&gt;A boolean TRUE value is converted to the string "1", the FALSE value is represented as "" (empty string). This way you can convert back and forth between boolean and string values. &lt;br /&gt;&lt;br /&gt;An integer or a floating point number (float) is converted to a string representing the number with its digits (including the exponent part for floating point numbers). &lt;br /&gt;&lt;br /&gt;Arrays are always converted to the string "Array", so you cannot dump out the contents of an array with echo() or print() to see what is inside them. To view one element, you'd do something like echo $arr['foo']. See below for tips on dumping/viewing the entire contents. &lt;br /&gt;&lt;br /&gt;Objects are always converted to the string "Object". If you would like to print out the member variable values of an object for debugging reasons, read the paragraphs below. If you would like to find out the class name of which an object is an instance of, use get_class(). As of PHP 5, __toString() method is used if applicable. &lt;br /&gt;&lt;br /&gt;Resources are always converted to strings with the structure "Resource id #1" where 1 is the unique number of the resource assigned by PHP during runtime. If you would like to get the type of the resource, use get_resource_type(). &lt;br /&gt;&lt;br /&gt;NULL is always converted to an empty string. &lt;br /&gt;&lt;br /&gt;As you can see above, printing out the arrays, objects or resources does not provide you any useful information about the values themselves. Look at the functions print_r() and var_dump() for better ways to print out values for debugging. &lt;br /&gt;&lt;br /&gt;You can also convert PHP values to strings to store them permanently. This method is called serialization, and can be done with the function serialize(). You can also serialize PHP values to XML structures, if you have WDDX support in your PHP setup. &lt;br /&gt;&lt;br /&gt;String conversion to numbers&lt;br /&gt;When a string is evaluated as a numeric value, the resulting value and type are determined as follows. &lt;br /&gt;&lt;br /&gt;The string will evaluate as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will evaluate as an integer. &lt;br /&gt;&lt;br /&gt;The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$foo = 1 + "10.5";                // $foo is float (11.5)&lt;br /&gt;$foo = 1 + "-1.3e3";              // $foo is float (-1299)&lt;br /&gt;$foo = 1 + "bob-1.3e3";           // $foo is integer (1)&lt;br /&gt;$foo = 1 + "bob3";                // $foo is integer (1)&lt;br /&gt;$foo = 1 + "10 Small Pigs";       // $foo is integer (11)&lt;br /&gt;$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)&lt;br /&gt;$foo = "10.0 pigs " + 1;          // $foo is float (11)&lt;br /&gt;$foo = "10.0 pigs " + 1.0;        // $foo is float (11)     &lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For more information on this conversion, see the Unix manual page for strtod(3). &lt;br /&gt;&lt;br /&gt;If you would like to test any of the examples in this section, you can cut and paste the examples and insert the following line to see for yourself what's going on: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br /&gt;\n";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Do not expect to get the code of one character by converting it to integer (as you would do in C for example). Use the functions ord() and chr() to convert between charcodes and characters. &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;????? ??? ???? ????? &lt;br /&gt;Floating point numbers ????? Arrays &lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-931052568946127709?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/931052568946127709/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-strings.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/931052568946127709'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/931052568946127709'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-strings.html' title='PHP strings'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-822809185315935923</id><published>2010-02-20T21:50:00.000-08:00</published><updated>2010-02-20T21:57:36.299-08:00</updated><title type='text'>PHP captcha security source code</title><content type='html'>&lt;?php&lt;br /&gt;session_start();&lt;br /&gt; &lt;br /&gt;/*&lt;br /&gt;* File: CaptchaSecurityImages.php&lt;br /&gt;* Author: Simon Jarvis&lt;br /&gt;* Copyright: 2006 Simon Jarvis&lt;br /&gt;* Date: 03/08/06&lt;br /&gt;* Updated: 07/02/07&lt;br /&gt;* Requirements: PHP 4/5 with GD and FreeType libraries&lt;br /&gt;* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php&lt;br /&gt;* &lt;br /&gt;* This program is free software; you can redistribute it and/or &lt;br /&gt;* modify it under the terms of the GNU General Public License &lt;br /&gt;* as published by the Free Software Foundation; either version 2 &lt;br /&gt;* of the License, or (at your option) any later version.&lt;br /&gt;* &lt;br /&gt;* This program is distributed in the hope that it will be useful, &lt;br /&gt;* but WITHOUT ANY WARRANTY; without even the implied warranty of &lt;br /&gt;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the &lt;br /&gt;* GNU General Public License for more details: &lt;br /&gt;* http://www.gnu.org/licenses/gpl.html&lt;br /&gt;*&lt;br /&gt;*/&lt;br /&gt; &lt;br /&gt;class CaptchaSecurityImages {&lt;br /&gt; &lt;br /&gt;   var $font = 'monofont.ttf';&lt;br /&gt; &lt;br /&gt;   function generateCode($characters) {&lt;br /&gt;      /* list all possible characters, similar looking characters and vowels have been removed */&lt;br /&gt;      $possible = '23456789bcdfghjkmnpqrstvwxyz';&lt;br /&gt;      $code = '';&lt;br /&gt;      $i = 0;&lt;br /&gt;      while ($i &lt; $characters) { &lt;br /&gt;         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);&lt;br /&gt;         $i++;&lt;br /&gt;      }&lt;br /&gt;      return $code;&lt;br /&gt;   }&lt;br /&gt; &lt;br /&gt;   function CaptchaSecurityImages($width='120',$height='40',$characters='6') {&lt;br /&gt;      $code = $this-&gt;generateCode($characters);&lt;br /&gt;      /* font size will be 75% of the image height */&lt;br /&gt;      $font_size = $height * 0.75;&lt;br /&gt;      $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');&lt;br /&gt;      /* set the colours */&lt;br /&gt;      $background_color = imagecolorallocate($image, 255, 255, 255);&lt;br /&gt;      $text_color = imagecolorallocate($image, 20, 40, 100);&lt;br /&gt;      $noise_color = imagecolorallocate($image, 100, 120, 180);&lt;br /&gt;      /* generate random dots in background */&lt;br /&gt;      for( $i=0; $i&lt;($width*$height)/3; $i++ ) {&lt;br /&gt;         imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);&lt;br /&gt;      }&lt;br /&gt;      /* generate random lines in background */&lt;br /&gt;      for( $i=0; $i&lt;($width*$height)/150; $i++ ) {&lt;br /&gt;         imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);&lt;br /&gt;      }&lt;br /&gt;      /* create textbox and add text */&lt;br /&gt;      $textbox = imagettfbbox($font_size, 0, $this-&gt;font, $code) or die('Error in imagettfbbox function');&lt;br /&gt;      $x = ($width - $textbox[4])/2;&lt;br /&gt;      $y = ($height - $textbox[5])/2;&lt;br /&gt;      imagettftext($image, $font_size, 0, $x, $y, $text_color, $this-&gt;font , $code) or die('Error in imagettftext function');&lt;br /&gt;      /* output captcha image to browser */&lt;br /&gt;      header('Content-Type: image/jpeg');&lt;br /&gt;      imagejpeg($image);&lt;br /&gt;      imagedestroy($image);&lt;br /&gt;      $_SESSION['security_code'] = $code;&lt;br /&gt;   }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;$width = isset($_GET['width']) &amp;&amp; $_GET['width'] &lt; 600 ? $_GET['width'] : '120';&lt;br /&gt;$height = isset($_GET['height']) &amp;&amp; $_GET['height'] &lt; 200 ? $_GET['height'] : '40';&lt;br /&gt;$characters = isset($_GET['characters']) &amp;&amp; $_GET['characters'] &gt; 2 ? $_GET['characters'] : '6';&lt;br /&gt; &lt;br /&gt;$captcha = new CaptchaSecurityImages($width,$height,$characters);&lt;br /&gt; &lt;br /&gt;?&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-822809185315935923?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/822809185315935923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-captcha-security-source-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/822809185315935923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/822809185315935923'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2010/02/php-captcha-security-source-code.html' title='PHP captcha security source code'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4423187073080136551</id><published>2009-08-03T09:35:00.000-07:00</published><updated>2009-08-03T09:36:35.078-07:00</updated><title type='text'>SQL Injection</title><content type='html'>Many web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands. &lt;br /&gt;&lt;br /&gt;Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately. &lt;br /&gt;&lt;br /&gt;Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database. ????? 27-2. Splitting the result set into pages ... and making superusers (PostgreSQL) &lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$offset = $argv[0]; // beware, no input validation!&lt;br /&gt;$query  = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";&lt;br /&gt;$result = pg_query($conn, $query);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;Normal users click on the 'next', 'prev' links where the $offset is encoded into the URL. The script expects that the incoming $offset is a decimal number. However, what if someone tries to break in by appending a urlencode()'d form of the following to the URL &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;0;&lt;br /&gt;insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd)&lt;br /&gt;    select 'crack', usesysid, 't','t','crack'&lt;br /&gt;    from pg_shadow where usename='postgres';&lt;br /&gt;--&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If it happened, then the script would present a superuser access to him. Note that 0; is to supply a valid offset to the original query and to terminate it. &lt;br /&gt;&lt;br /&gt;????: It is common technique to force the SQL parser to ignore the rest of the query written by the developer with -- which is the comment sign in SQL. &lt;br /&gt;&lt;br /&gt;A feasible way to gain passwords is to circumvent your search result pages. The only thing the attacker needs to do is to see if there are any submitted variables used in SQL statements which are not handled properly. These filters can be set commonly in a preceding form to customize WHERE, ORDER BY, LIMIT and OFFSET clauses in SELECT statements. If your database supports the UNION construct, the attacker may try to append an entire query to the original one to list passwords from an arbitrary table. Using encrypted password fields is strongly encouraged. ????? 27-3. Listing out articles ... and some passwords (any database server) &lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$query  = "SELECT id, name, inserted, size FROM products&lt;br /&gt;                  WHERE size = '$size'&lt;br /&gt;                  ORDER BY $order LIMIT $limit, $offset;";&lt;br /&gt;$result = odbc_exec($conn, $query);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;The static part of the query can be combined with another SELECT statement which reveals all passwords: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;&lt;br /&gt;--&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If this query (playing with the ' and --) were assigned to one of the variables used in $query, the query beast awakened. &lt;br /&gt;&lt;br /&gt;SQL UPDATE's are also susceptible to attack. These queries are also threatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the SET clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examining the form variable names, or just simply brute forcing. There are not so many naming conventions for fields storing passwords or usernames. ????? 27-4. From resetting a password ... to gaining more privileges (any database server) &lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';";&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;But a malicious user sumbits the value ' or uid like'%admin%'; -- to $uid to change the admin's password, or simply sets $pwd to "hehehe', admin='yes', trusted=100 " (with a trailing space) to gain more privileges. Then, the query will be twisted: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;// $uid == ' or uid like'%admin%'; --&lt;br /&gt;$query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --";&lt;br /&gt;&lt;br /&gt;// $pwd == "hehehe', admin='yes', trusted=100 "&lt;br /&gt;$query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE&lt;br /&gt;...;";&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A frightening example how operating system level commands can be accessed on some database hosts. ????? 27-5. Attacking the database hosts operating system (MSSQL Server)&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$query  = "SELECT * FROM products WHERE id LIKE '%$prod%'";&lt;br /&gt;$result = mssql_query($query);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;If attacker submits the value a%' exec master..xp_cmdshell 'net user test testpass /ADD' -- to $prod, then the $query will be: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$query  = "SELECT * FROM products&lt;br /&gt;                    WHERE id LIKE '%a%'&lt;br /&gt;                    exec master..xp_cmdshell 'net user test testpass /ADD'--";&lt;br /&gt;$result = mssql_query($query);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MSSQL Server executes the SQL statements in the batch including a command to add a new user to the local accounts database. If this application were running as sa and the MSSQLSERVER service is running with sufficient privileges, the attacker would now have an account with which to access this machine. &lt;br /&gt;&lt;br /&gt;????: Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be similarly vulnerable in another manner. &lt;br /&gt;&lt;br /&gt;Avoiding techniques&lt;br /&gt;You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management system or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one. &lt;br /&gt;&lt;br /&gt;These attacks are mainly based on exploiting the code not being written with security in mind. Never trust any kind of input, especially that which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Never connect to the database as a superuser or as the database owner. Use always customized users with very limited privileges. &lt;br /&gt;&lt;br /&gt;Check if the given input has the expected data type. PHP has a wide range of input validating functions, from the simplest ones found in Variable Functions and in Character Type Functions (e.g. is_numeric(), ctype_digit() respectively) and onwards to the Perl compatible Regular Expressions support. &lt;br /&gt;&lt;br /&gt;If the application waits for numerical input, consider verifying data with is_numeric(), or silently change its type using settype(), or use its numeric representation by sprintf(). ????? 27-6. A more secure way to compose a query for paging&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;settype($offset, 'integer');&lt;br /&gt;$query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";&lt;br /&gt;&lt;br /&gt;// please note %d in the format string, using %s would be meaningless&lt;br /&gt;$query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;",&lt;br /&gt;                 $offset);&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Quote each non numeric user supplied value that is passed to the database with the database-specific string escape function (e.g. mysql_escape_string(), sql_escape_string(), etc.). If a database-specific string escape mechanism is not available, the addslashes() and str_replace() functions may be useful (depending on database type). See the first example. As the example shows, adding quotes to the static part of the query is not enough, making this query easily crackable. &lt;br /&gt;&lt;br /&gt;Do not print out any database specific information, especially about the schema, by fair means or foul. See also Error Reporting and Error Handling and Logging Functions. &lt;br /&gt;&lt;br /&gt;You may use stored procedures and previously defined cursors to abstract data access so that users do not directly access tables or views, but this solution has another impacts. &lt;br /&gt;&lt;br /&gt;Besides these, you benefit from logging queries either within your script or by the database itself, if it supports logging. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself, but through the information it contains. More detail is generally better than less&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4423187073080136551?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4423187073080136551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/sql-injection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4423187073080136551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4423187073080136551'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/sql-injection.html' title='SQL Injection'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4795118383770686810</id><published>2009-08-03T09:34:00.000-07:00</published><updated>2009-08-03T09:35:12.505-07:00</updated><title type='text'>Encrypted Storage Model</title><content type='html'>SSL/SSH protects data travelling from the client to the server, SSL/SSH does not protect the persistent data stored in a database. SSL is an on-the-wire protocol. &lt;br /&gt;&lt;br /&gt;Once an attacker gains access to your database directly (bypassing the webserver), the stored sensitive data may be exposed or misused, unless the information is protected by the database itself. Encrypting the data is a good way to mitigate this threat, but very few databases offer this type of data encryption. &lt;br /&gt;&lt;br /&gt;The easiest way to work around this problem is to first create your own encryption package, and then use it from within your PHP scripts. PHP can assist you in this with several extensions, such as Mcrypt and Mhash, covering a wide variety of encryption algorithms. The script encrypts the data before inserting it into the database, and decrypts it when retrieving. See the references for further examples of how encryption works. &lt;br /&gt;&lt;br /&gt;In case of truly hidden data, if its raw representation is not needed (i.e. not be displayed), hashing may also be taken into consideration. The well-known example for the hashing is storing the MD5 hash of a password in a database, instead of the password itself. See also crypt() and md5(). &lt;br /&gt;&lt;br /&gt;????? 27-1. Using hashed password field&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;// storing password hash&lt;br /&gt;$query  = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",&lt;br /&gt;            pg_escape_string($username), md5($password));&lt;br /&gt;$result = pg_query($connection, $query);&lt;br /&gt;&lt;br /&gt;// querying if user submitted the right password&lt;br /&gt;$query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';",&lt;br /&gt;            pg_escape_string($username), md5($password));&lt;br /&gt;$result = pg_query($connection, $query);&lt;br /&gt;&lt;br /&gt;if (pg_num_rows($result) &gt; 0) {&lt;br /&gt;    echo 'Welcome, $username!';&lt;br /&gt;} else {&lt;br /&gt;    echo 'Authentication failed for $username.';&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4795118383770686810?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4795118383770686810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/encrypted-storage-model.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4795118383770686810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4795118383770686810'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/encrypted-storage-model.html' title='Encrypted Storage Model'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6764203418401084283</id><published>2009-08-03T09:31:00.000-07:00</published><updated>2009-08-03T09:33:57.409-07:00</updated><title type='text'>Database Security</title><content type='html'>Nowadays, databases are cardinal components of any web based application by enabling websites to provide varying dynamic content. Since very sensitive or secret information can be stored in a database, you should strongly consider protecting your databases. &lt;br /&gt;&lt;br /&gt;To retrieve or to store any information you need to connect to the database, send a legitimate query, fetch the result, and close the connection. Nowadays, the commonly used query language in this interaction is the Structured Query Language (SQL). See how an attacker can tamper with an SQL query. &lt;br /&gt;&lt;br /&gt;As you can surmise, PHP cannot protect your database by itself. The following sections aim to be an introduction into the very basics of how to access and manipulate databases within PHP scripts. &lt;br /&gt;&lt;br /&gt;Keep in mind this simple rule: defense in depth. The more places you take action to increase the protection of your database, the less probability of an attacker succeeding in exposing or abusing any stored information. Good design of the database schema and the application deals with your greatest fears. &lt;br /&gt;&lt;br /&gt;Designing Databases&lt;br /&gt;The first step is always to create the database, unless you want to use one from a third party. When a database is created, it is assigned to an owner, who executed the creation statement. Usually, only the owner (or a superuser) can do anything with the objects in that database, and in order to allow other users to use it, privileges must be granted. &lt;br /&gt;&lt;br /&gt;Applications should never connect to the database as its owner or a superuser, because these users can execute any query at will, for example, modifying the schema (e.g. dropping tables) or deleting its entire content. &lt;br /&gt;&lt;br /&gt;You may create different database users for every aspect of your application with very limited rights to database objects. The most required privileges should be granted only, and avoid that the same user can interact with the database in different use cases. This means that if intruders gain access to your database using your applications credentials, they can only effect as many changes as your application can. &lt;br /&gt;&lt;br /&gt;You are encouraged not to implement all the business logic in the web application (i.e. your script), instead do it in the database schema using views, triggers or rules. If the system evolves, new ports will be intended to open to the database, and you have to re-implement the logic in each separate database client. Over and above, triggers can be used to transparently and automatically handle fields, which often provides insight when debugging problems with your application or tracing back transactions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6764203418401084283?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6764203418401084283/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/database-security.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6764203418401084283'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6764203418401084283'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/database-security.html' title='Database Security'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4740579806969435723</id><published>2009-08-03T09:30:00.000-07:00</published><updated>2009-08-03T09:31:13.879-07:00</updated><title type='text'>Magic Quotes</title><content type='html'>Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed. &lt;br /&gt;&lt;br /&gt;What are Magic Quotes&lt;br /&gt;When on, all ' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does. &lt;br /&gt;&lt;br /&gt;There are three magic quote directives: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;magic_quotes_gpc &lt;br /&gt;&lt;br /&gt;Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP. &lt;br /&gt;&lt;br /&gt;See also get_magic_quotes_gpc(). &lt;br /&gt;&lt;br /&gt;magic_quotes_runtime &lt;br /&gt;&lt;br /&gt;If enabled, most functions that return data from an external source, including databases and text files, will have quotes escaped with a backslash. Can be set at runtime, and defaults to off in PHP. &lt;br /&gt;&lt;br /&gt;See also set_magic_quotes_runtime() and get_magic_quotes_runtime(). &lt;br /&gt;&lt;br /&gt;magic_quotes_sybase &lt;br /&gt;&lt;br /&gt;If enabled, a single-quote is escaped with a single-quote instead of a backslash. If on, it completely overrides magic_quotes_gpc. Having both directives enabled means only single quotes are escaped as ''. Double quotes, backslashes and NULL's will remain untouched and unescaped. &lt;br /&gt;&lt;br /&gt;See also ini_get() for retrieving its value.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4740579806969435723?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4740579806969435723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/magic-quotes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4740579806969435723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4740579806969435723'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/magic-quotes.html' title='Magic Quotes'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4300074723589624510</id><published>2009-08-03T09:29:00.000-07:00</published><updated>2009-08-03T09:30:25.296-07:00</updated><title type='text'>Using Register Globals</title><content type='html'>Perhaps the most controversial change in PHP is when the default value for the PHP directive register_globals went from ON to OFF in PHP 4.2.0. Reliance on this directive was quite common and many people didn't even know it existed and assumed it's just how PHP works. This page will explain how one can write insecure code with this directive but keep in mind that the directive itself isn't insecure but rather it's the misuse of it. &lt;br /&gt;&lt;br /&gt;When on, register_globals will inject your scripts with all sorts of variables, like request variables from HTML forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this. Let's demonstrate with an example misuse of register_globals: &lt;br /&gt;&lt;br /&gt;????? 29-1. Example misuse with register_globals = on&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// define $authorized = true only if user is authenticated&lt;br /&gt;if (authenticated_user()) {&lt;br /&gt;    $authorized = true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Because we didn't first initialize $authorized as false, this might be&lt;br /&gt;// defined through register_globals, like from GET auth.php?authorized=1&lt;br /&gt;// So, anyone can be seen as authenticated!&lt;br /&gt;if ($authorized) {&lt;br /&gt;    include "/highly/sensitive/data.php";&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When register_globals = on, our logic above may be compromised. When off, $authorized can't be set via request so it'll be fine, although it really is generally a good programming practice to initialize variables first. For example, in our example above we might have first done $authorized = false. Doing this first means our above code would work with register_globals on or off as users by default would be unauthorized. &lt;br /&gt;&lt;br /&gt;Another example is that of sessions. When register_globals = on, we could also use $username in our example below but again you must realize that $username could also come from other means, such as GET (through the URL). &lt;br /&gt;&lt;br /&gt;????? 29-2. Example use of sessions with register_globals on or off&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// We wouldn't know where $username came from but do know $_SESSION is&lt;br /&gt;// for session data&lt;br /&gt;if (isset($_SESSION['username'])) {&lt;br /&gt;&lt;br /&gt;    echo "Hello &lt;b&gt;{$_SESSION['username']}&lt;/b&gt;";&lt;br /&gt;&lt;br /&gt;} else {&lt;br /&gt;&lt;br /&gt;    echo "Hello &lt;b&gt;Guest&lt;/b&gt;&lt;br /&gt;";&lt;br /&gt;    echo "Would you like to login?";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It's even possible to take preventative measures to warn when forging is being attempted. If you know ahead of time exactly where a variable should be coming from, you can check to see if the submitted data is coming from an inappropriate kind of submission. While it doesn't guarantee that data has not been forged, it does require an attacker to guess the right kind of forging. If you don't care where the request data comes from, you can use $_REQUEST as it contains a mix of GET, POST and COOKIE data. See also the manual section on using variables from outside of PHP. &lt;br /&gt;&lt;br /&gt;????? 29-3. Detecting simple variable poisoning&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;if (isset($_COOKIE['MAGIC_COOKIE'])) {&lt;br /&gt;&lt;br /&gt;    // MAGIC_COOKIE comes from a cookie.&lt;br /&gt;    // Be sure to validate the cookie data!&lt;br /&gt;&lt;br /&gt;} elseif (isset($_GET['MAGIC_COOKIE']) || isset($_POST['MAGIC_COOKIE'])) {&lt;br /&gt;&lt;br /&gt;   mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']);&lt;br /&gt;   echo "Security violation, admin has been alerted.";&lt;br /&gt;   exit;&lt;br /&gt;&lt;br /&gt;} else {&lt;br /&gt;&lt;br /&gt;   // MAGIC_COOKIE isn't set through this REQUEST&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Of course, simply turning off register_globals does not mean your code is secure. For every piece of data that is submitted, it should also be checked in other ways. Always validate your user data and initialize your variables! To check for uninitialized variables you may turn up error_reporting() to show E_NOTICE level errors.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4300074723589624510?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4300074723589624510/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/using-register-globals.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4300074723589624510'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4300074723589624510'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/using-register-globals.html' title='Using Register Globals'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7306705150971341564</id><published>2009-08-03T09:28:00.001-07:00</published><updated>2009-08-03T09:28:58.144-07:00</updated><title type='text'>Filesystem Security</title><content type='html'>PHP is subject to the security built into most server systems with respect to permissions on a file and directory basis. This allows you to control which files in the filesystem may be read. Care should be taken with any files which are world readable to ensure that they are safe for reading by all users who have access to that filesystem. &lt;br /&gt;&lt;br /&gt;Since PHP was designed to allow user level access to the filesystem, it's entirely possible to write a PHP script that will allow you to read system files such as /etc/passwd, modify your ethernet connections, send massive printer jobs out, etc. This has some obvious implications, in that you need to ensure that the files that you read from and write to are the appropriate ones. &lt;br /&gt;&lt;br /&gt;Consider the following script, where a user indicates that they'd like to delete a file in their home directory. This assumes a situation where a PHP web interface is regularly used for file management, so the Apache user is allowed to delete files in the user home directories. &lt;br /&gt;&lt;br /&gt;????? 26-1. Poor variable checking leads to....&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// remove a file from the user's home directory&lt;br /&gt;$username = $_POST['user_submitted_name'];&lt;br /&gt;$homedir = "/home/$username";&lt;br /&gt;$file_to_delete = "$userfile";&lt;br /&gt;unlink ("$homedir/$userfile");&lt;br /&gt;echo "$file_to_delete has been deleted!";&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;Since the username is postable from a user form, they can submit a username and file belonging to someone else, and delete files. In this case, you'd want to use some other form of authentication. Consider what could happen if the variables submitted were "../etc/" and "passwd". The code would then effectively read: ????? 26-2. ... A filesystem attack&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// removes a file from anywhere on the hard drive that&lt;br /&gt;// the PHP user has access to. If PHP has root access:&lt;br /&gt;$username = "../etc/";&lt;br /&gt;$homedir = "/home/../etc/";&lt;br /&gt;$file_to_delete = "passwd";&lt;br /&gt;unlink ("/home/../etc/passwd");&lt;br /&gt;echo "/home/../etc/passwd has been deleted!";&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;There are two important measures you should take to prevent these issues. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Only allow limited permissions to the PHP web user binary. &lt;br /&gt;&lt;br /&gt;Check all variables which are submitted. &lt;br /&gt;&lt;br /&gt;Here is an improved script: ????? 26-3. More secure file name checking&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// removes a file from the hard drive that&lt;br /&gt;// the PHP user has access to.&lt;br /&gt;$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim&lt;br /&gt;&lt;br /&gt;$homedir = "/home/$username";&lt;br /&gt;&lt;br /&gt;$file_to_delete = basename("$userfile"); // strip paths&lt;br /&gt;unlink ($homedir/$file_to_delete);&lt;br /&gt;&lt;br /&gt;$fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion&lt;br /&gt;$logstring = "$username $homedir $file_to_delete";&lt;br /&gt;fwrite ($fp, $logstring);&lt;br /&gt;fclose($fp);&lt;br /&gt;&lt;br /&gt;echo "$file_to_delete has been deleted!";&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;However, even this is not without it's flaws. If your authentication system allowed users to create their own user logins, and a user chose the login "../etc/", the system is once again exposed. For this reason, you may prefer to write a more customized check: ????? 26-4. More secure file name checking&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim&lt;br /&gt;$homedir = "/home/$username";&lt;br /&gt;&lt;br /&gt;if (!ereg('^[^./][^/]*$', $userfile))&lt;br /&gt;     die('bad filename'); //die, do not process&lt;br /&gt;&lt;br /&gt;if (!ereg('^[^./][^/]*$', $username))&lt;br /&gt;     die('bad username'); //die, do not process&lt;br /&gt;//etc...&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Depending on your operating system, there are a wide variety of files which you should be concerned about, including device entries (/dev/ or COM1), configuration files (/etc/ files and the .ini files), well known file storage areas (/home/, My Documents), etc. For this reason, it's usually easier to create a policy where you forbid everything except for what you explicitly allow.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7306705150971341564?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7306705150971341564/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/filesystem-security.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7306705150971341564'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7306705150971341564'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/filesystem-security.html' title='Filesystem Security'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6918574728318923811</id><published>2009-08-03T09:25:00.000-07:00</published><updated>2009-08-03T09:28:05.218-07:00</updated><title type='text'>Security</title><content type='html'>PHP is a powerful language and the interpreter, whether included in a web server as a module or executed as a separate CGI binary, is able to access files, execute commands and open network connections on the server. These properties make anything run on a web server insecure by default. PHP is designed specifically to be a more secure language for writing CGI programs than Perl or C, and with correct selection of compile-time and runtime configuration options, and proper coding practices, it can give you exactly the combination of freedom and security you need. &lt;br /&gt;&lt;br /&gt;As there are many different ways of utilizing PHP, there are many configuration options controlling its behaviour. A large selection of options guarantees you can use PHP for a lot of purposes, but it also means there are combinations of these options and server configurations that result in an insecure setup. &lt;br /&gt;&lt;br /&gt;The configuration flexibility of PHP is equally rivalled by the code flexibility. PHP can be used to build complete server applications, with all the power of a shell user, or it can be used for simple server-side includes with little risk in a tightly controlled environment. How you build that environment, and how secure it is, is largely up to the PHP developer. &lt;br /&gt;&lt;br /&gt;This chapter starts with some general security advice, explains the different configuration option combinations and the situations they can be safely used, and describes different considerations in coding for different levels of security.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6918574728318923811?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6918574728318923811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/security.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6918574728318923811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6918574728318923811'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/08/security.html' title='Security'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-1130172117552986541</id><published>2009-07-05T08:22:00.000-07:00</published><updated>2009-07-05T08:23:02.623-07:00</updated><title type='text'>string wordwrap ( string source [, int width [, string break [, boolean cut]]])</title><content type='html'>string number_format ( float number [, int decimal_places])&lt;br /&gt;&lt;br /&gt;string number_format ( float number, int decimal_places, string decimal_point, string thousands_seperator)&lt;br /&gt;&lt;br /&gt;Number_format() is a remarkably helpful function that takes a minimum of one parameter, the number to format, and returns that same number with grouped thousands. There are two function prototypes for number_format() as you either pass it one, two, or four parameters - passing it one or two fits the first prototype, and passing four fits the second.&lt;br /&gt;&lt;br /&gt;So, if you pass number_format() a parameter of "1234567", it will return "1,234,567". By default, number_format() rounds fractions - 1234567.89 becomes 1,234,568. However, you can change this by specifying the second parameter, which is the number of decimal places to include. Parameter three allows you to choose the character to use as your decimal point, and parameter four allows you to choose the character to use as your thousands separator. Here is how it all looks in PHP:&lt;br /&gt;&lt;?php&lt;br /&gt;    $num = 12345.6789;&lt;br /&gt;    $a = number_format($num);&lt;br /&gt;    $b = number_format($num, 3);&lt;br /&gt;    $c = number_format($num, 4, ',', '.');&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;After running that script, $a will be set to 12,346, $b will be set to 12,345.679, and $c will be set to 12.345,6789 (periods used to separate thousands, and commas used for the decimal point, east European-style).&lt;br /&gt;&lt;br /&gt;As you can imagine, number_format() is incredibly useful when it comes to formatting money for checkout pages in shopping baskets, although it is useful anywhere you need to represent large numbers - adding a thousand separator invariably makes things easier to read.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-1130172117552986541?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/1130172117552986541/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/string-wordwrap-string-source-int-width.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1130172117552986541'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1130172117552986541'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/string-wordwrap-string-source-int-width.html' title='string wordwrap ( string source [, int width [, string break [, boolean cut]]])'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-909277072513769459</id><published>2009-07-05T08:21:00.001-07:00</published><updated>2009-07-05T08:21:56.827-07:00</updated><title type='text'>Wrapping your lines</title><content type='html'>string wordwrap ( string source [, int width [, string break [, boolean cut]]])&lt;br /&gt;&lt;br /&gt;Although web pages wrap text automatically, there are two situations when you might want to wrap text yourself:&lt;br /&gt;&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      When printing to a console as opposed to a web page, text does not wrap automatically. Therefore, unless you want your users to scroll around a lot, it is best to wrap text for them.&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      When printing to a web page that has been designed to exactly accommodate a certain width of text, allowing browsers to wrap text whenever they want will likely lead to the design getting warped.&lt;br /&gt;&lt;br /&gt;In either of these situations, the wordwrap() function comes to your aid. If you pass a sentence of text into wordwrap() with no other parameters, it will return that same string wrapped at the 75-character mark using "\n" for new lines. However, you can pass both the size and new line marker as parameters two and three if you want to, like this:&lt;br /&gt;&lt;?php&lt;br /&gt;    $text = "Word wrap will split this text up into smaller lines, which makes for easier reading and neater layout.";&lt;br /&gt;    $text = wordwrap($text, 20, "&lt;br /&gt;");&lt;br /&gt;    print $text;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Running that script will give you the following output:&lt;br /&gt;Word wrap will split&lt;br /&gt;this text up into&lt;br /&gt;smaller lines, which&lt;br /&gt;makes for easier&lt;br /&gt;reading and neater&lt;br /&gt;layout.&lt;br /&gt;&lt;br /&gt;As you can see, wordwrap() has used &lt;br /&gt;, a HTML new line marker, and split up words at the 20-character mark. Note that wordwrap() always pessimistically wraps words - that is, if you set the second parameter to 20, wordwrap() will always wrap when it hits 20 characters or under - not 21, 22, etc. The only exception to this is if you have words that are individually longer than 20 characters - wordwrap() will not break up a word, and so may return larger chunks than the limit you set.&lt;br /&gt;&lt;br /&gt;If you really want your limit to be a hard maximum, you can supply 1 as a fourth parameter, which enables "cut" mode - words over the limit will be cut up if this is enabled. Here is an example of cut mode in action:&lt;br /&gt;&lt;?php&lt;br /&gt;    $text = "Micro-organism is a very long word.";&lt;br /&gt;    $text = wordwrap($text, 6, "\n", 1);&lt;br /&gt;    print $text;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That will output the following:&lt;br /&gt;Micro-&lt;br /&gt;organi&lt;br /&gt;sm is&lt;br /&gt;a very&lt;br /&gt;long&lt;br /&gt;word.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-909277072513769459?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/909277072513769459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/wrapping-your-lines.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/909277072513769459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/909277072513769459'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/wrapping-your-lines.html' title='Wrapping your lines'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-2131524716224598266</id><published>2009-07-05T08:20:00.000-07:00</published><updated>2009-07-05T08:21:10.363-07:00</updated><title type='text'>Finding a string within a string</title><content type='html'>Strpos(), and its case-insensitive sibling stripos(), returns the index of the first occurrence of a substring within a string. It is easier to explain in code, so here goes:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "This is a strpos() test";&lt;br /&gt;    print strpos($string, "a") . "\n";&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That will return 8, because the first character in "This is a strpos() test" that is a lowercase A is at index 8. Remember that PHP considers the first letter of a string to be index 0, which means that the A strpos() found is actually the ninth character.&lt;br /&gt;&lt;br /&gt;You can specify whole words in parameter two, which will make strpos() return the first position of that word within the string, for example strpos($string, "test") would return 19 - the index of the first letter in the matched word.&lt;br /&gt;&lt;br /&gt;If the substring sent in parameter two is not found in parameter one, strpos() will return false. Consider this script:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "This is a strpos() test";&lt;br /&gt;    $pos = strpos($string, "This");&lt;br /&gt;    if ($pos == false) {&lt;br /&gt;        print "Not found\n";&lt;br /&gt;    } else {&lt;br /&gt;        print "Found!\n";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;If you try executing that, you will find that it outputs "Not found", despite "This" quite clearly being in $string. Is it another case sensitivity problem? Not quite. This time the problem lies in the fact that "This" is the first thing in $string, which means that strpos() will return 0. However, PHP considers 0 to be the same value as false, which means that our if statement cannot tell the difference between "Substring not found" and "Substring found at index 0" - quite a problem!&lt;br /&gt;&lt;br /&gt;Luckily, PHP comes to the rescue with the === operator, which, if you recall, means "is identical to", which means $pos must be equal to false and of the same type as false (boolean). If the "This" is found in $string, strpos()will return 0, but it will be of type integer . If we change our if statement to use === rather than ==, PHP will check the value of 0 and false and find they match (both false), then check the types of 0 and false, and find that they do not match - the former is an integer, and the latter is a boolean.&lt;br /&gt;&lt;br /&gt;So, the corrected version of the script is this:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "This is a strpos() test";&lt;br /&gt;    $pos = strpos($string, "This");&lt;br /&gt;    if ($pos === false) {&lt;br /&gt;        print "Not found\n";&lt;br /&gt;    } else {&lt;br /&gt;        print "Found!\n";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Now, consider this next script, which tries to match the "i" in "is":&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "This is a strpos() test";&lt;br /&gt;    $pos = strpos($string, "i");&lt;br /&gt;    if ($pos === false) {&lt;br /&gt;        print "Not found\n";&lt;br /&gt;    } else {&lt;br /&gt;        print "Found at $pos!\n";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;The problem there is that strpos() matches the first "i" it comes across, which will be in "This". Fortunately there is a third parameter to strpos() that allows us to specify where to start from. As the "i" in "This" is at index 2, we just need to specify one place after that (3) as the start position for strpos(), and it will report back the next "i" after it. For example:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "This is a strpos() test";&lt;br /&gt;    $pos = strpos($string, "i", 3);&lt;br /&gt;    if ($pos === false) {&lt;br /&gt;        print "Not found\n";&lt;br /&gt;    } else {&lt;br /&gt;        print "Found at $pos!\n";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;This time that will print "found at 5!", which is the position of the "i" in "is".&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-2131524716224598266?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/2131524716224598266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/finding-string-within-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2131524716224598266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2131524716224598266'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/finding-string-within-string.html' title='Finding a string within a string'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3186992776344873136</id><published>2009-07-05T08:18:00.000-07:00</published><updated>2009-07-05T08:19:12.003-07:00</updated><title type='text'>Padding out a string</title><content type='html'>string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])&lt;br /&gt;&lt;br /&gt;Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "Goodbye, Perl!";&lt;br /&gt;    $newstring = str_pad($string, 10);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.&lt;br /&gt;&lt;br /&gt;Str_pad() has an optional third parameter that lets you set the padding character to use, so:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "Goodbye, Perl!";&gt;&lt;br /&gt;    $newstring = str_pad($string, 10, 'a');&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.&lt;br /&gt;&lt;br /&gt;We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "Goodbye, Perl!";&lt;br /&gt;    $a = str_pad($string, 10, '-', STR_PAD_LEFT);&lt;br /&gt;    $b = str_pad($string, 10, '-', STR_PAD_RIGHT);&lt;br /&gt;    $c = str_pad($string, 10, '-', STR_PAD_BOTH);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.&lt;br /&gt;&lt;br /&gt;Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use "&amp;nbsp;", the HTML code for non-breaking space.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3186992776344873136?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3186992776344873136/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/padding-out-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3186992776344873136'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3186992776344873136'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/padding-out-string.html' title='Padding out a string'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7483088554946243785</id><published>2009-07-05T08:17:00.002-07:00</published><updated>2009-07-05T08:18:23.717-07:00</updated><title type='text'>Parsing a string into variables</title><content type='html'>void parse_str ( string input [, array store])&lt;br /&gt;&lt;br /&gt;Previously we looked at a handful of the variables set for you inside the superglobal arrays, of which one was QUERY_STRING. If you recall, this is the literal text sent after the question mark in a HTTP GET request, which means that if the page requested was "mypage.php?foo=bar&amp;bar=baz", QUERY_STRING is set to "foo=bar&amp;bar=baz".&lt;br /&gt;&lt;br /&gt;The parse_str() function is designed to take a query string like that one and convert it to variables in the same way that PHP does when variables come in. The difference is that variables parsed using parse_str() are converted to global variables, as opposed to elements inside $_GET. So:&lt;br /&gt;&lt;?php&lt;br /&gt;    if (isset($foo)) {&lt;br /&gt;        print "Foo is $foo&lt;br /&gt;";&lt;br /&gt;    } else {&lt;br /&gt;        print "Foo is unset&lt;br /&gt;";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    parse_str("foo=bar&amp;bar=baz");&lt;br /&gt;&lt;br /&gt;    if (isset($foo)) {&lt;br /&gt;        print "Foo is $foo&lt;br /&gt;";&lt;br /&gt;    } else {&lt;br /&gt;        print "Foo is unset&lt;br /&gt;";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That will print out "Foo is unset" followed by "Foo is bar", because the call to parse_str() will set $foo to "bar" and $bar to "baz". Optionally, you can pass an array as the second parameter to parse_str(), and it will put the variables into there. That would make the script look like this:&lt;br /&gt;&lt;?php&lt;br /&gt;    $array = array();&lt;br /&gt;&lt;br /&gt;    if (isset($array['foo'])) {&lt;br /&gt;        print "Foo is {$array['foo']}&lt;br /&gt;";&lt;br /&gt;    } else {&lt;br /&gt;        print "Foo is unset&lt;br /&gt;";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    parse_str("foo=bar&amp;bar=baz", $array);&lt;br /&gt;&lt;br /&gt;    if (isset($array['foo'])) {&lt;br /&gt;        print "Foo is {$array['foo']}&lt;br /&gt;";&lt;br /&gt;    } else {&lt;br /&gt;        print "Foo is unset&lt;br /&gt;";&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That script outputs the same as before, except that the variables found in the query string are placed into $array. As you can see, the variable names are used as keys in the array and their values are used as the array values.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7483088554946243785?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7483088554946243785/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/parsing-string-into-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7483088554946243785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7483088554946243785'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/parsing-string-into-variables.html' title='Parsing a string into variables'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6143000939214674689</id><published>2009-07-05T08:17:00.001-07:00</published><updated>2009-07-05T08:17:29.833-07:00</updated><title type='text'>Regular expression syntax examples</title><content type='html'>In order to give you a quick reference to the different patterns and what they will match, here's a comprehensive table of all we've covered. Column one contains example expressions, and column two contains what that expression will match.&lt;br /&gt;&lt;br /&gt;Expr&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Will match...&lt;br /&gt;&lt;br /&gt;foo&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;the string "foo"&lt;br /&gt;&lt;br /&gt;^foo&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;"foo" at the start of a line&lt;br /&gt;&lt;br /&gt;foo$&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;"foo" at the end of a line&lt;br /&gt;&lt;br /&gt;^foo$&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;"foo" when it is alone on a line&lt;br /&gt;&lt;br /&gt;[Ff]oo&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;"Foo" or "foo"&lt;br /&gt;&lt;br /&gt;[abc]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;a, b, or c&lt;br /&gt;&lt;br /&gt;[^abc]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;d, e, f, g, h, etc - everything that is not a, b, or c (^ is "not" inside sets)&lt;br /&gt;&lt;br /&gt;[A-Z]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any uppercase letter&lt;br /&gt;&lt;br /&gt;[a-z]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any lowercase letter&lt;br /&gt;&lt;br /&gt;[A-Za-z]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any letter&lt;br /&gt;&lt;br /&gt;[A-Za-z0-9]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any letter of number&lt;br /&gt;&lt;br /&gt;[A-Z]+&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;one or more uppercase letters&lt;br /&gt;&lt;br /&gt;[A-Z]*&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;zero or more uppercase letters&lt;br /&gt;&lt;br /&gt;[A-Z]?&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;zero or one uppercase letters&lt;br /&gt;&lt;br /&gt;[A-Z]{3}&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;3 uppercase letters&lt;br /&gt;&lt;br /&gt;[A-Z]{3,}&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;a minimum of 3 uppercase letters&lt;br /&gt;&lt;br /&gt;[A-Z]{1,3}&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;1-3 uppercase letters&lt;br /&gt;&lt;br /&gt;[^0-9]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any non-numeric character&lt;br /&gt;&lt;br /&gt;[^0-9A-Za-z]&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any symbol (not a number or a letter)&lt;br /&gt;&lt;br /&gt;Fo*&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;F, Fo, Foo, Fooo, Foooo, etc&lt;br /&gt;&lt;br /&gt;Fo+&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Fo, Foo, Fooo, Foooo, etc&lt;br /&gt;&lt;br /&gt;Fo?&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;F, Fo&lt;br /&gt;&lt;br /&gt;.&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any character except \n (new line)&lt;br /&gt;&lt;br /&gt;\b&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;a word boundary. E.g. te\b matches the "te" in "late", but not the "te" in "tell".&lt;br /&gt;&lt;br /&gt;\B&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;a non-word boundary. "te\B" matches the "te" in "tell" but not the "te" in "late".&lt;br /&gt;&lt;br /&gt;\n&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;new line character&lt;br /&gt;&lt;br /&gt;\s&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any whitespace (new line, space, tab, etc)&lt;br /&gt;&lt;br /&gt;\S&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;any non-whitespace character&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6143000939214674689?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6143000939214674689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/regular-expression-syntax-examples.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6143000939214674689'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6143000939214674689'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/regular-expression-syntax-examples.html' title='Regular expression syntax examples'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5851082256187126405</id><published>2009-07-05T08:16:00.001-07:00</published><updated>2009-07-05T08:16:43.847-07:00</updated><title type='text'>Checking whether a function is available</title><content type='html'>bool function_exists ( string function_name)&lt;br /&gt;&lt;br /&gt;If you're working with functions that are not part of the core of PHP, that is, functions that are from an extension that needs to be enabled by users, it's a smart move to use the function_exists() function. This takes a function name as its only parameter, and returns true if that function (either built-in, or one you've defined yourself) is available for use. Note that it only checks whether the function is available, not whether it will work - your system may not be configured properly for some functions.&lt;br /&gt;&lt;br /&gt;Author's Note: If you ever want to know whether you have a function available to you, use the function_exists() function. This takes one string parameter that is the name of a function, and returns true if the function exists or false if it does not. Many people use function_exists() to find out whether they have an extension available, by calling function_exists() on a function of that extension. However, this is accomplished much more easily with the function extension_loaded() function covered later.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5851082256187126405?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5851082256187126405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/checking-whether-function-is-available.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5851082256187126405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5851082256187126405'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/checking-whether-function-is-available.html' title='Checking whether a function is available'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3173727081040183878</id><published>2009-07-05T08:15:00.002-07:00</published><updated>2009-07-05T08:16:13.162-07:00</updated><title type='text'>Changing string case</title><content type='html'>string strtoupper ( string source)&lt;br /&gt;&lt;br /&gt;string strtolower ( string source)&lt;br /&gt;&lt;br /&gt;string ucfirst ( string source)&lt;br /&gt;&lt;br /&gt;string ucwords ( string source)&lt;br /&gt;&lt;br /&gt;Strtoupper() is part of a small family of functions that affect the case of characters of strings. Strtoupper() takes one string parameter, and returns that string entirely in uppercase. Other variations include strtolower(), to convert the string to lowercase, ucfirst() to convert the first letter of every string to uppercase, and ucwords(), to convert the first letter of every word in the string to uppercase. They all take one parameter and return the converted result, so once you learn one you have learnt them all:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "i like to program in PHP";&lt;br /&gt;    $a = strtoupper($string);&lt;br /&gt;    $b = strtolower($string);&lt;br /&gt;    $c = ucfirst($string);&lt;br /&gt;    $d = ucwords($string);&lt;br /&gt;    $e = ucwords(strtolower($string));&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Each of those variables get set to a slightly different value: $a becomes "I LIKE TO PROGAM IN PHP", $b becomes "i like to program in php", $c becomes "I like to program in PHP", $d becomes "I Like To Program In PHP", and $e becomes "I Like To Program In Php".&lt;br /&gt;&lt;br /&gt;From that, you should be able to see that in calls such as ucwords(), PHP will not change existing capital letters to lowercase, which is why $d and $e are different - for $e, all the letters are lowercased first, then passed through ucwords() to make PHP into Php. &lt;br /&gt;&lt;br /&gt;string strtoupper ( string source)&lt;br /&gt;&lt;br /&gt;string strtolower ( string source)&lt;br /&gt;&lt;br /&gt;string ucfirst ( string source)&lt;br /&gt;&lt;br /&gt;string ucwords ( string source)&lt;br /&gt;&lt;br /&gt;Strtoupper() is part of a small family of functions that affect the case of characters of strings. Strtoupper() takes one string parameter, and returns that string entirely in uppercase. Other variations include strtolower(), to convert the string to lowercase, ucfirst() to convert the first letter of every string to uppercase, and ucwords(), to convert the first letter of every word in the string to uppercase. They all take one parameter and return the converted result, so once you learn one you have learnt them all:&lt;br /&gt;&lt;?php&lt;br /&gt;    $string = "i like to program in PHP";&lt;br /&gt;    $a = strtoupper($string);&lt;br /&gt;    $b = strtolower($string);&lt;br /&gt;    $c = ucfirst($string);&lt;br /&gt;    $d = ucwords($string);&lt;br /&gt;    $e = ucwords(strtolower($string));&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Each of those variables get set to a slightly different value: $a becomes "I LIKE TO PROGAM IN PHP", $b becomes "i like to program in php", $c becomes "I like to program in PHP", $d becomes "I Like To Program In PHP", and $e becomes "I Like To Program In Php".&lt;br /&gt;&lt;br /&gt;From that, you should be able to see that in calls such as ucwords(), PHP will not change existing capital letters to lowercase, which is why $d and $e are different - for $e, all the letters are lowercased first, then passed through ucwords() to make PHP into Php.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3173727081040183878?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3173727081040183878/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/changing-string-case.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3173727081040183878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3173727081040183878'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/changing-string-case.html' title='Changing string case'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4564313342718422427</id><published>2009-07-05T08:15:00.001-07:00</published><updated>2009-07-05T08:15:43.180-07:00</updated><title type='text'>Trimming whitespace</title><content type='html'>string trim ( string source [, string charlist])&lt;br /&gt;&lt;br /&gt;string ltrim ( string source [, string charlist])&lt;br /&gt;&lt;br /&gt;string rtrim ( string source [, string charlist])&lt;br /&gt;&lt;br /&gt;Trim() is a function to strip whitespace from either side of a string variable, with "whitespace" meaning spaces, new lines, and tabs. That is, if you have the string " This is a test " and pass it to trim() as its first parameter, it will return the string "This is a test" - the same thing, but with the spaces trimmed off the end.&lt;br /&gt;&lt;br /&gt;You can pass an optional second parameter to trim() if you want, which should be a string specifying the characters you want it to trim(). For example, if we were to pass to trim the second parameter " tes" (that starts with a space), it would output "This is a" - the test would be trimmed, as well as the spaces. As you can see, trim() is again case sensitive - the T in "This" is left untouched.&lt;br /&gt;&lt;br /&gt;Trim() has two minor variant functions, ltrim() and rtrim(), which do the same thing but only trim from the left and right respectively.&lt;br /&gt;&lt;br /&gt;Here are some examples:&lt;br /&gt;&lt;?php&lt;br /&gt;    $a = trim(" testing ");&lt;br /&gt;    $b = trim(" testing ", " teng");&lt;br /&gt;    $c = ltrim(" testing ");&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;$a will result in "testing", $b will result in "sti", and $c will result in "testing " - as expected, and not surprising because trim() et al are simple to use.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4564313342718422427?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4564313342718422427/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/trimming-whitespace.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4564313342718422427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4564313342718422427'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/trimming-whitespace.html' title='Trimming whitespace'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-2664225191881261134</id><published>2009-07-05T08:14:00.001-07:00</published><updated>2009-07-05T08:14:56.302-07:00</updated><title type='text'>Return values</title><content type='html'>You're allowed to return one and only one value back from functions, and you do this by using the return statement. In our example, we could have used "return 'foo';" or "return 10 + 10;" to pass other values back, but "return 1;" is easiest, and usually the most common as it is the same as "return true;"&lt;br /&gt;&lt;br /&gt;You can return any variable you want, as long as it is just one variable - it can be an integer, a string, a database connection, etc. The "return" keyword sets up the function return value to be whatever variable you use with it, then exits the function immediately. You can also just use "return;", which means "exit without sending a value back."&lt;br /&gt;&lt;br /&gt;Consider this script:&lt;br /&gt;&lt;?php&lt;br /&gt;    function foo() {&lt;br /&gt;        print "In function";&lt;br /&gt;        return 1;&lt;br /&gt;        print "Leaving function...";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    print foo();&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;That will output "In function", followed by "1", and then the script will terminate. The reason we never see "Leaving function..." is because the line "return 1" passes one back then immediately exits - the second print statement in foo() is never reached.&lt;br /&gt;&lt;br /&gt;If you want to pass more than one value back, you need to use an array - this is covered soon.&lt;br /&gt;&lt;br /&gt;A popular thing to do is to return the value of a conditional statement, e.g.:&lt;br /&gt;return $i &gt; 10;&lt;br /&gt;&lt;br /&gt;If $i is indeed greater than 10, the &gt; operator will return 1, so it is the same as having "return 1", but if $i is less than or equal to ten, it is the same as being "return 0".&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-2664225191881261134?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/2664225191881261134/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/return-values.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2664225191881261134'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2664225191881261134'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/return-values.html' title='Return values'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6107968587110890736</id><published>2009-07-05T08:13:00.000-07:00</published><updated>2009-07-05T08:14:21.272-07:00</updated><title type='text'>Variable functions</title><content type='html'>bool is_callable ( mixed function_name [, bool syntax_only [, string callable_name]])&lt;br /&gt;&lt;br /&gt;mixed call_user_func ( callback function [, mixed parameter [, mixed ...]])&lt;br /&gt;&lt;br /&gt;mixed call_user_func_array ( callback function [, array parameters])&lt;br /&gt;&lt;br /&gt;As you have seen already, PHP has variable variables so it is not surprising we have variable functions. This particular piece of clever functionality allows you to write code like this:&lt;br /&gt;&lt;?php&lt;br /&gt;    $func = "sqrt";&lt;br /&gt;    print $func(49);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;PHP sees that you are calling a function using a variable, looks up the value of the variable, then calls the matching function. The code above will therefore return 7 - the square root of 49.&lt;br /&gt;&lt;br /&gt;As variable functions are quite unusual and also easy to get wrong, there is a special PHP function, is_callable(), that takes a string as its only parameter and returns true if that string contains a function name that can be called using a variable function. Thus, our script becomes this:&lt;br /&gt;&lt;?php&lt;br /&gt;    $func = "sqrt";&lt;br /&gt;    if (is_callable($func)) {&lt;br /&gt;        print $func(49);&lt;br /&gt;    }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;As an alternative to variable functions, you can use call_user_func() and call_user_func_array(), which take the function to call as their first parameter. The difference between the two is that call_user_func() takes the parameters to pass into the variable function as multiple parameters to itself, whereas call_user_func_array() takes an array of parameters as its second parameter.&lt;br /&gt;&lt;br /&gt;This next script demonstrates both of these two performing a functionally similar operation, replacing "monkeys" with "giraffes" in a sentence using str_replace():&lt;br /&gt;&lt;?php&lt;br /&gt;    $func = "str_replace";&lt;br /&gt;    $output_single = call_user_func($func, "monkeys", "giraffes", "Hundreds and thousands of monkeys\n");&lt;br /&gt;    $params = array("monkeys", "giraffes", "Hundreds and thousands of monkeys\n");&lt;br /&gt;    $output_array = call_user_func_array($func, $params);&lt;br /&gt;    echo $output_single;&lt;br /&gt;    echo $output_array;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Although call_user_func() is essentially the same as using a variable function, call_user_func_array() is very helpful for functions that have complex and variable parameter requirements. One popular application for variable functions is to allow other developers using your code to register callbacks - they pass in the name of the function they want your code to call, then you can use call_user_func() to execute that.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6107968587110890736?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6107968587110890736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6107968587110890736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6107968587110890736'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-functions.html' title='Variable functions'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8681109280070314532</id><published>2009-07-05T08:12:00.000-07:00</published><updated>2009-07-05T08:13:19.477-07:00</updated><title type='text'>Overriding scope with the GLOBALS array</title><content type='html'>At some point in your PHP programming career you will want to read a global variable inside a function - I can pretty much guarantee that, because it is a very popular thing to do. Luckily, it is made easy for you by PHP through the $GLOBALS superglobal array, which allows you to access global variables even from within functions. When it comes to the $GLOBALS array it is quite simple: all variables declared in the global scope are in the $GLOBALS array, which you can access anywhere in the script.&lt;br /&gt;&lt;br /&gt;To demonstrate this in action, consider the following script:&lt;br /&gt;&lt;?php&lt;br /&gt;    function foo() {&lt;br /&gt;        $GLOBALS['bar'] = "wombat";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    $bar = "baz";&lt;br /&gt;    foo();&lt;br /&gt;    print $bar;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;What do you think that will output this time? If you guessed "wombat", you would be correct - the foo() function literally alters a variable outside of its scope, so that even after it returns control back to the main script, its effect is still felt. You can of course read variables in the same way, like this:&lt;br /&gt;$localbar = $GLOBALS['bar'];&lt;br /&gt;&lt;br /&gt;However, that is quite hard on the eyes. PHP allows you to use a special keyword, GLOBAL, to allow a variable to be accessed locally. For example:&lt;br /&gt;function myfunc() {&lt;br /&gt;    GLOBAL $foo, $bar, $baz;&lt;br /&gt;    ++$baz;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;That would allow a function to read the global variables $foo, $bar, and $baz. The ++$baz line will increment $baz by 1, and this will be reflected in the global scope also.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8681109280070314532?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8681109280070314532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/overriding-scope-with-globals-array.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8681109280070314532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8681109280070314532'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/overriding-scope-with-globals-array.html' title='Overriding scope with the GLOBALS array'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6312393771384792203</id><published>2009-07-05T08:10:00.001-07:00</published><updated>2009-07-05T08:10:53.556-07:00</updated><title type='text'>Variable scope in functions</title><content type='html'>As mentioned already, variables declared outside of functions and classes are considered "global" - generally available to the script. However, as functions are independent blocks, their variables are self-contained and do not affect variables in the main script. In the same way, variables from the main script are not implicitly made available inside functions. Take a look at this example script:&lt;br /&gt;&lt;?php&lt;br /&gt;    function foo() {&lt;br /&gt;        $bar = "wombat";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    $bar = "baz";&lt;br /&gt;    foo();&lt;br /&gt;    print $bar;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Execution of the script starts at the $bar = "baz" line, and then calls the foo() function. Now, as you can see, foo() sets $bar to "wombat", then returns control to the main script where $bar is printed out. Consider for a moment what you think that script will do, taking into account what I have just said regarding variable scope in functions.&lt;br /&gt;&lt;br /&gt;There are, overall, three possibilities:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;&lt;br /&gt;      The script will print "baz"&lt;br /&gt;   2.&lt;br /&gt;&lt;br /&gt;      The script will print "wombat"&lt;br /&gt;   3.&lt;br /&gt;&lt;br /&gt;      The script will print nothing&lt;br /&gt;&lt;br /&gt;Possibility one would be the case if the $bar variable was set outside of the function, foo() was called and set its own, local version of $bar, which was deleted once the function ended, leaving the original $bar in place.&lt;br /&gt;&lt;br /&gt;Possibility two would be the case if the $bar variable was set outside of the function, foo() was called, and changed the global copy of $bar, therefore printing out the new value once control returns to the main script.&lt;br /&gt;&lt;br /&gt;Possibility three would be the case if variables are lost in between function calls.&lt;br /&gt;&lt;br /&gt;It is quite simple to discount the third possibility - variables declared globally, that is, outside of functions, remain in the global scope, no matter what functions you call.&lt;br /&gt;&lt;br /&gt;The second possibility would mean that variables declared globally are automatically made available inside functions, which we know is not the case. Therefore, the first possibility is in fact correct - foo() is called, and, having no knowledge that a $bar variable exists in the global scope, creates a $bar variable in its local scope. Once the function ends, all local scopes are tossed away, leaving the original $bar variable intact.&lt;br /&gt;&lt;br /&gt;For many this procedure is second nature, however it does take a little getting used to if you are new to programming, which is why I have gone into so much depth. This explicit level of scope is something you will find is particularly important once you go beyond simple scripts.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6312393771384792203?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6312393771384792203/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-scope-in-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6312393771384792203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6312393771384792203'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-scope-in-functions.html' title='Variable scope in functions'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-8783649067902049126</id><published>2009-07-05T08:09:00.000-07:00</published><updated>2009-07-05T08:10:16.899-07:00</updated><title type='text'>Variable parameter counts</title><content type='html'>int func_num_args ( )&lt;br /&gt;&lt;br /&gt;mixed func_get_arg ( int arg_num)&lt;br /&gt;&lt;br /&gt;array func_get_args ( )&lt;br /&gt;&lt;br /&gt;The printf() function we examined previously is able to take an arbitrary number of parameters. That is, it could take just one parameter, or five, or fifty, or five hundred - it can take as many as are passed into it by the user. This is known as a variable-length parameter list, and it is automatically implemented in your user functions. For example:&lt;br /&gt;&lt;?php&lt;br /&gt;    function some_func($a, $b) {&lt;br /&gt;        $j = 1;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    some_func(1,2,3,4,5,6,7,8);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Here the function some_func() is defined to only take two parameters, $a and $b, but we call it with eight parameters and the script should run without a problem. In that example, 1 will be placed into $a, and 2 will be placed into $b, but what happens to the other parameters?&lt;br /&gt;&lt;br /&gt;Coming to your rescue are three functions: func_num_args(), func_get_arg(), and func_get_args(), of which the first and last take no parameters. To get the number of arguments that were passed into your function, call func_num_args() and read its return value. To get the value of an individual parameter, use func_get_arg() and pass in the parameter number you want to retrieve to have its value returned back to you. Finally, func_get_args() returns an array of the parameters that were passed in. Here's an example:&lt;br /&gt;&lt;?php&lt;br /&gt;    function some_func($a, $b) {&lt;br /&gt;        for ($i = 0; $i &lt; func_num_args(); ++$i) {&lt;br /&gt;            $param = func_get_arg($i);&lt;br /&gt;            echo "Received parameter $param.\n";&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    function some_other_func($a, $b) {&lt;br /&gt;        $param = func_get_args();&lt;br /&gt;        $param = join(", ", $param);&lt;br /&gt;        echo "Received parameters: $param.\n";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    some_func(1,2,3,4,5,6,7,8);&lt;br /&gt;    some_other_func(1,2,3,4,5,6,7,8);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Using func_num_args() alone you can easily implement function error checking. You can, for example, start off each of your functions by checking to make sure func_num_args() is what you are expecting, and, if not, exit. Once you add func_get_arg() into the mix, however, you should easily be able to create your own functions that work with any number of parameters.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-8783649067902049126?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/8783649067902049126/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-parameter-counts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8783649067902049126'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/8783649067902049126'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/variable-parameter-counts.html' title='Variable parameter counts'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-2686811626678314534</id><published>2009-07-05T08:08:00.000-07:00</published><updated>2009-07-05T08:09:09.793-07:00</updated><title type='text'>Functions</title><content type='html'>Functions, both ones built into PHP and ones you define yourself, make coding much easier - they take away lots of hard work because you can reuse other people's code, and they allow you to keep your scripts shorter and easier to maintain. As PHP 5 includes more than 2,500 functions, you might assume it's a very easy language indeed, but the truth is that each function needs to be used in different ways and so needs to be learnt individually. In this chapter you will learn your first PHP functions, with the most helpful and easy first.&lt;br /&gt;&lt;br /&gt;Rather than writing pieces of code time after time whenever you want to execute the same functionality, PHP allows you to encapsulate code into a named function that you can call from elsewhere in your script.&lt;br /&gt;&lt;br /&gt;PHP comes with hundreds of predefined functions that perform all manner of tasks from reading files and manipulating strings up to querying databases and connecting to an IRC server. If you find something is missing, you can add your own functions on a script by script basis, and these are called user functions .&lt;br /&gt;&lt;br /&gt;In this section we will be covering a variety of the most important basic functions in PHP - more specialised functions can be found spread throughout the book under various sections, and should be looked up using the index.&lt;br /&gt;&lt;br /&gt;Topics covered in this chapter are:&lt;br /&gt;&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Working with date and time&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Mathematical functions&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      String manipulation&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Creating data hashes&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Regular expressions&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Extension handling&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Writing your own functions&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Recursive, variable, and callback functions&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-2686811626678314534?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/2686811626678314534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2686811626678314534'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2686811626678314534'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/functions.html' title='Functions'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-1873461754941921101</id><published>2009-07-04T11:40:00.000-07:00</published><updated>2009-07-04T11:41:40.999-07:00</updated><title type='text'>Arrays</title><content type='html'>An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees. &lt;br /&gt;&lt;br /&gt;Explanation of those data structures is beyond the scope of this manual, but you'll find at least one example for each of them. For more information we refer you to external literature about this broad topic. &lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;Specifying with array()&lt;br /&gt;An array can be created by the array() language-construct. It takes a certain number of comma-separated key =&gt; value pairs. &lt;br /&gt;&lt;br /&gt;array( [key =&gt;] value&lt;br /&gt;     , ...&lt;br /&gt;     )&lt;br /&gt;// key may be an integer or string&lt;br /&gt;// value may be any value&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$arr = array("foo" =&gt; "bar", 12 =&gt; true);&lt;br /&gt;&lt;br /&gt;echo $arr["foo"]; // bar&lt;br /&gt;echo $arr[12];    // 1&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices. &lt;br /&gt;&lt;br /&gt;A value can be of any PHP type. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$arr = array("somearray" =&gt; array(6 =&gt; 5, 13 =&gt; 9, "a" =&gt; 42));&lt;br /&gt;&lt;br /&gt;echo $arr["somearray"][6];    // 5&lt;br /&gt;echo $arr["somearray"][13];   // 9&lt;br /&gt;echo $arr["somearray"]["a"];  // 42&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// This array is the same as ...&lt;br /&gt;array(5 =&gt; 43, 32, 56, "b" =&gt; 12);&lt;br /&gt;&lt;br /&gt;// ...this array&lt;br /&gt;array(5 =&gt; 43, 6 =&gt; 32, 7 =&gt; 56, "b" =&gt; 12);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;As of PHP 4.3.0, the index generation behaviour described above has changed. Now, if you append to an array in which the current maximum key is negative, then the next key created will be zero (0). Before, the new index would have been set to the largest existing key + 1, the same as positive indices are. &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Using TRUE as a key will evaluate to integer 1 as key. Using FALSE as a key will evaluate to integer 0 as key. Using NULL as a key will evaluate to the empty string. Using the empty string as key will create (or overwrite) a key with the empty string and its value; it is not the same as using empty brackets. &lt;br /&gt;&lt;br /&gt;You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type. &lt;br /&gt;&lt;br /&gt;Creating/modifying with square-bracket syntax&lt;br /&gt;You can also modify an existing array by explicitly setting values in it. &lt;br /&gt;&lt;br /&gt;This is done by assigning values to the array while specifying the key in brackets. You can also omit the key, add an empty pair of brackets ("[]") to the variable name in that case. $arr[key] = value;&lt;br /&gt;$arr[] = value;&lt;br /&gt;// key may be an integer or string&lt;br /&gt;// value may be any value&lt;br /&gt; &lt;br /&gt;If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array. To change a certain value, just assign a new value to an element specified with its key. If you want to remove a key/value pair, you need to unset() it. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$arr = array(5 =&gt; 1, 12 =&gt; 2);&lt;br /&gt;&lt;br /&gt;$arr[] = 56;    // This is the same as $arr[13] = 56;&lt;br /&gt;                // at this point of the script&lt;br /&gt;&lt;br /&gt;$arr["x"] = 42; // This adds a new element to&lt;br /&gt;                // the array with key "x"&lt;br /&gt;                &lt;br /&gt;unset($arr[5]); // This removes the element from the array&lt;br /&gt;&lt;br /&gt;unset($arr);    // This deletes the whole array&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: As mentioned above, if you provide the brackets with no key specified, then the maximum of the existing integer indices is taken, and the new key will be that maximum value + 1 . If no integer indices exist yet, the key will be 0 (zero). If you specify a key that already has a value assigned to it, that value will be overwritten. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;As of PHP 4.3.0, the index generation behaviour described above has changed. Now, if you append to an array in which the current maximum key is negative, then the next key created will be zero (0). Before, the new index would have been set to the largest existing key + 1, the same as positive indices are. &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that the maximum integer key used for this need not currently exist in the array. It simply must have existed in the array at some time since the last time the array was re-indexed. The following example illustrates: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Create a simple array.&lt;br /&gt;$array = array(1, 2, 3, 4, 5);&lt;br /&gt;print_r($array);&lt;br /&gt;&lt;br /&gt;// Now delete every item, but leave the array itself intact:&lt;br /&gt;foreach ($array as $i =&gt; $value) {&lt;br /&gt;    unset($array[$i]);&lt;br /&gt;}&lt;br /&gt;print_r($array);&lt;br /&gt;&lt;br /&gt;// Append an item (note that the new key is 5, instead of 0 as you&lt;br /&gt;// might expect).&lt;br /&gt;$array[] = 6;&lt;br /&gt;print_r($array);&lt;br /&gt;&lt;br /&gt;// Re-index:&lt;br /&gt;$array = array_values($array);&lt;br /&gt;$array[] = 7;&lt;br /&gt;print_r($array);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; 1&lt;br /&gt;    [1] =&gt; 2&lt;br /&gt;    [2] =&gt; 3&lt;br /&gt;    [3] =&gt; 4&lt;br /&gt;    [4] =&gt; 5&lt;br /&gt;)&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;)&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [5] =&gt; 6&lt;br /&gt;)&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; 6&lt;br /&gt;    [1] =&gt; 7&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Useful functions&lt;br /&gt;There are quite a few useful functions for working with arrays. See the array functions section. &lt;br /&gt;&lt;br /&gt;????: The unset() function allows unsetting keys of an array. Be aware that the array will NOT be reindexed. If you only use "usual integer indices" (starting from zero, increasing by one), you can achieve the reindex effect by using array_values(). &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = array(1 =&gt; 'one', 2 =&gt; 'two', 3 =&gt; 'three');&lt;br /&gt;unset($a[2]);&lt;br /&gt;/* will produce an array that would have been defined as&lt;br /&gt;   $a = array(1 =&gt; 'one', 3 =&gt; 'three');&lt;br /&gt;   and NOT&lt;br /&gt;   $a = array(1 =&gt; 'one', 2 =&gt;'three');&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;$b = array_values($a);&lt;br /&gt;// Now $b is array(0 =&gt; 'one', 1 =&gt;'three')&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The foreach control structure exists specifically for arrays. It provides an easy way to traverse an array. &lt;br /&gt;&lt;br /&gt;Array do's and don'ts&lt;br /&gt;Why is $foo[bar] wrong?&lt;br /&gt;You should always use quotes around a string literal array index. For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar] wrong? You might have seen the following syntax in old scripts: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$foo[bar] = 'enemy';&lt;br /&gt;echo $foo[bar];&lt;br /&gt;// etc&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is wrong, but it works. Then, why is it wrong? The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes), and PHP may in future define constants which, unfortunately for your code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that. &lt;br /&gt;&lt;br /&gt;????: This does not mean to always quote the key. You do not want to quote keys which are constants or variables, as this will prevent PHP from interpreting them. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;ini_set('display_errors', true);&lt;br /&gt;ini_set('html_errors', false);&lt;br /&gt;// Simple array:&lt;br /&gt;$array = array(1, 2);&lt;br /&gt;$count = count($array);&lt;br /&gt;for ($i = 0; $i &lt; $count; $i++) {&lt;br /&gt;    echo "\nChecking $i: \n";&lt;br /&gt;    echo "Bad: " . $array['$i'] . "\n";&lt;br /&gt;    echo "Good: " . $array[$i] . "\n";&lt;br /&gt;    echo "Bad: {$array['$i']}\n";&lt;br /&gt;    echo "Good: {$array[$i]}\n";&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: The above example will output:&lt;br /&gt;&lt;br /&gt;Checking 0: &lt;br /&gt;Notice: Undefined index:  $i in /path/to/script.html on line 9&lt;br /&gt;Bad: &lt;br /&gt;Good: 1&lt;br /&gt;Notice: Undefined index:  $i in /path/to/script.html on line 11&lt;br /&gt;Bad: &lt;br /&gt;Good: 1&lt;br /&gt;&lt;br /&gt;Checking 1: &lt;br /&gt;Notice: Undefined index:  $i in /path/to/script.html on line 9&lt;br /&gt;Bad: &lt;br /&gt;Good: 2&lt;br /&gt;Notice: Undefined index:  $i in /path/to/script.html on line 11&lt;br /&gt;Bad: &lt;br /&gt;Good: 2&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;More examples to demonstrate this fact: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Let's show all errors&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;&lt;br /&gt;$arr = array('fruit' =&gt; 'apple', 'veggie' =&gt; 'carrot');&lt;br /&gt;&lt;br /&gt;// Correct&lt;br /&gt;print $arr['fruit'];  // apple&lt;br /&gt;print $arr['veggie']; // carrot&lt;br /&gt;&lt;br /&gt;// Incorrect.  This works but also throws a PHP error of&lt;br /&gt;// level E_NOTICE because of an undefined constant named fruit&lt;br /&gt;// &lt;br /&gt;// Notice: Use of undefined constant fruit - assumed 'fruit' in...&lt;br /&gt;print $arr[fruit];    // apple&lt;br /&gt;&lt;br /&gt;// Let's define a constant to demonstrate what's going on.  We&lt;br /&gt;// will assign value 'veggie' to a constant named fruit.&lt;br /&gt;define('fruit', 'veggie');&lt;br /&gt;&lt;br /&gt;// Notice the difference now&lt;br /&gt;print $arr['fruit'];  // apple&lt;br /&gt;print $arr[fruit];    // carrot&lt;br /&gt;&lt;br /&gt;// The following is okay as it's inside a string.  Constants are not&lt;br /&gt;// looked for within strings so no E_NOTICE error here&lt;br /&gt;print "Hello $arr[fruit]";      // Hello apple&lt;br /&gt;&lt;br /&gt;// With one exception, braces surrounding arrays within strings&lt;br /&gt;// allows constants to be looked for&lt;br /&gt;print "Hello {$arr[fruit]}";    // Hello carrot&lt;br /&gt;print "Hello {$arr['fruit']}";  // Hello apple&lt;br /&gt;&lt;br /&gt;// This will not work, results in a parse error such as:&lt;br /&gt;// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'&lt;br /&gt;// This of course applies to using autoglobals in strings as well&lt;br /&gt;print "Hello $arr['fruit']";&lt;br /&gt;print "Hello $_GET['foo']";&lt;br /&gt;&lt;br /&gt;// Concatenation is another option&lt;br /&gt;print "Hello " . $arr['fruit']; // Hello apple&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When you turn error_reporting() up to show E_NOTICE level errors (such as setting it to E_ALL) then you will see these errors. By default, error_reporting is turned down to not show them. &lt;br /&gt;&lt;br /&gt;As stated in the syntax section, there must be an expression between the square brackets ('[' and ']'). That means that you can write things like this: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo $arr[somefunc($bar)];&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is an example of using a function return value as the array index. PHP also knows about constants, as you may have seen the E_* ones before. &lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$error_descriptions[E_ERROR]   = "A fatal error has occured";&lt;br /&gt;$error_descriptions[E_WARNING] = "PHP issued a warning";&lt;br /&gt;$error_descriptions[E_NOTICE]  = "This is just an informal notice";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that E_ERROR is also a valid identifier, just like bar in the first example. But the last example is in fact the same as writing: &lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$error_descriptions[1] = "A fatal error has occured";&lt;br /&gt;$error_descriptions[2] = "PHP issued a warning";&lt;br /&gt;$error_descriptions[8] = "This is just an informal notice";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;because E_ERROR equals 1, etc. &lt;br /&gt;&lt;br /&gt;As we already explained in the above examples, $foo[bar] still works but is wrong. It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes. &lt;br /&gt;&lt;br /&gt;So why is it bad then?&lt;br /&gt;At some point in the future, the PHP team might want to add another constant or keyword, or you may introduce another constant into your application, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special reserved keywords. &lt;br /&gt;&lt;br /&gt;????: To reiterate, inside a double-quoted string, it's valid to not surround array indexes with quotes so "$foo[bar]" is valid. See the above examples for details on why as well as the section on variable parsing in strings. &lt;br /&gt;&lt;br /&gt;Converting to array&lt;br /&gt;For any of the types: integer, float, string, boolean and resource, if you convert a value to an array, you get an array with one element (with index 0), which is the scalar value you started with. &lt;br /&gt;&lt;br /&gt;If you convert an object to an array, you get the properties (member variables) of that object as the array's elements. The keys are the member variable names. &lt;br /&gt;&lt;br /&gt;If you convert a NULL value to an array, you get an empty array. &lt;br /&gt;&lt;br /&gt;Comparing&lt;br /&gt;It is possible to compare arrays by array_diff() and by Array operators. &lt;br /&gt;&lt;br /&gt;Examples&lt;br /&gt;The array type in PHP is very versatile, so here will be some examples to show you the full power of arrays. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// this&lt;br /&gt;$a = array( 'color' =&gt; 'red',&lt;br /&gt;            'taste' =&gt; 'sweet',&lt;br /&gt;            'shape' =&gt; 'round',&lt;br /&gt;            'name'  =&gt; 'apple',&lt;br /&gt;                       4        // key will be 0&lt;br /&gt;          );&lt;br /&gt;&lt;br /&gt;// is completely equivalent with&lt;br /&gt;$a['color'] = 'red';&lt;br /&gt;$a['taste'] = 'sweet';&lt;br /&gt;$a['shape'] = 'round';&lt;br /&gt;$a['name']  = 'apple';&lt;br /&gt;$a[]        = 4;        // key will be 0&lt;br /&gt;&lt;br /&gt;$b[] = 'a';&lt;br /&gt;$b[] = 'b';&lt;br /&gt;$b[] = 'c';&lt;br /&gt;// will result in the array array(0 =&gt; 'a' , 1 =&gt; 'b' , 2 =&gt; 'c'),&lt;br /&gt;// or simply array('a', 'b', 'c')&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? 11-6. Using array()&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Array as (property-)map&lt;br /&gt;$map = array( 'version'    =&gt; 4,&lt;br /&gt;              'OS'         =&gt; 'Linux',&lt;br /&gt;              'lang'       =&gt; 'english',&lt;br /&gt;              'short_tags' =&gt; true&lt;br /&gt;            );&lt;br /&gt;            &lt;br /&gt;// strictly numerical keys&lt;br /&gt;$array = array( 7,&lt;br /&gt;                8,&lt;br /&gt;                0,&lt;br /&gt;                156,&lt;br /&gt;                -10&lt;br /&gt;              );&lt;br /&gt;// this is the same as array(0 =&gt; 7, 1 =&gt; 8, ...)&lt;br /&gt;&lt;br /&gt;$switching = array(         10, // key = 0&lt;br /&gt;                    5    =&gt;  6,&lt;br /&gt;                    3    =&gt;  7, &lt;br /&gt;                    'a'  =&gt;  4,&lt;br /&gt;                            11, // key = 6 (maximum of integer-indices was 5)&lt;br /&gt;                    '8'  =&gt;  2, // key = 8 (integer!)&lt;br /&gt;                    '02' =&gt; 77, // key = '02'&lt;br /&gt;                    0    =&gt; 12  // the value 10 will be overwritten by 12&lt;br /&gt;                  );&lt;br /&gt;                  &lt;br /&gt;// empty array&lt;br /&gt;$empty = array();         &lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;????? 11-7. Collection&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$colors = array('red', 'blue', 'green', 'yellow');&lt;br /&gt;&lt;br /&gt;foreach ($colors as $color) {&lt;br /&gt;    echo "Do you like $color?\n";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Do you like red?&lt;br /&gt;Do you like blue?&lt;br /&gt;Do you like green?&lt;br /&gt;Do you like yellow?&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Changing values of the array directly is possible since PHP 5 by passing them as reference. Prior versions need workaround: ????? 11-8. Collection&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// PHP 5&lt;br /&gt;foreach ($colors as &amp;$color) {&lt;br /&gt;    $color = strtoupper($color);&lt;br /&gt;}&lt;br /&gt;unset($color); /* ensure that following writes to&lt;br /&gt;$color will not modify the last array element */&lt;br /&gt;&lt;br /&gt;// Workaround for older versions&lt;br /&gt;foreach ($colors as $key =&gt; $color) {&lt;br /&gt;    $colors[$key] = strtoupper($color);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print_r($colors);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; RED&lt;br /&gt;    [1] =&gt; BLUE&lt;br /&gt;    [2] =&gt; GREEN&lt;br /&gt;    [3] =&gt; YELLOW&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This example creates a one-based array. ????? 11-9. One-based index&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$firstquarter  = array(1 =&gt; 'January', 'February', 'March');&lt;br /&gt;print_r($firstquarter);&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;The above example will output:&lt;br /&gt;&lt;br /&gt;Array &lt;br /&gt;(&lt;br /&gt;    [1] =&gt; 'January'&lt;br /&gt;    [2] =&gt; 'February'&lt;br /&gt;    [3] =&gt; 'March'&lt;br /&gt;)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? 11-10. Filling an array&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// fill an array with all items from a directory&lt;br /&gt;$handle = opendir('.');&lt;br /&gt;while (false !== ($file = readdir($handle))) {&lt;br /&gt;    $files[] = $file;&lt;br /&gt;}&lt;br /&gt;closedir($handle); &lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Arrays are ordered. You can also change the order using various sorting functions. See the array functions section for more information. You can count the number of items in an array using the count() function. &lt;br /&gt;&lt;br /&gt;????? 11-11. Sorting an array&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;sort($files);&lt;br /&gt;print_r($files);&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Because the value of an array can be anything, it can also be another array. This way you can make recursive and multi-dimensional arrays. &lt;br /&gt;&lt;br /&gt;????? 11-12. Recursive and multi-dimensional arrays&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$fruits = array ( "fruits"  =&gt; array ( "a" =&gt; "orange",&lt;br /&gt;                                       "b" =&gt; "banana",&lt;br /&gt;                                       "c" =&gt; "apple"&lt;br /&gt;                                     ),&lt;br /&gt;                  "numbers" =&gt; array ( 1,&lt;br /&gt;                                       2,&lt;br /&gt;                                       3,&lt;br /&gt;                                       4,&lt;br /&gt;                                       5,&lt;br /&gt;                                       6&lt;br /&gt;                                     ),&lt;br /&gt;                  "holes"   =&gt; array (      "first",&lt;br /&gt;                                       5 =&gt; "second",&lt;br /&gt;                                            "third"&lt;br /&gt;                                     )&lt;br /&gt;                );&lt;br /&gt;&lt;br /&gt;// Some examples to address values in the array above &lt;br /&gt;echo $fruits["holes"][5];    // prints "second"&lt;br /&gt;echo $fruits["fruits"]["a"]; // prints "orange"&lt;br /&gt;unset($fruits["holes"][0]);  // remove "first"&lt;br /&gt;&lt;br /&gt;// Create a new multi-dimensional array&lt;br /&gt;$juices["apple"]["green"] = "good"; &lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;You should be aware that array assignment always involves value copying. It also means that the internal array pointer used by current() and similar functions is reset. You need to use the reference operator to copy an array by reference. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$arr1 = array(2, 3);&lt;br /&gt;$arr2 = $arr1;&lt;br /&gt;$arr2[] = 4; // $arr2 is changed,&lt;br /&gt;             // $arr1 is still array(2, 3)&lt;br /&gt;             &lt;br /&gt;$arr3 = &amp;$arr1;&lt;br /&gt;$arr3[] = 4; // now $arr1 and $arr3 are the same&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;????? ??? ???? ????? &lt;br /&gt;Strings ????? Objects&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-1873461754941921101?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/1873461754941921101/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1873461754941921101'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1873461754941921101'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/arrays.html' title='Arrays'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7838795723511527548</id><published>2009-07-04T11:30:00.000-07:00</published><updated>2009-07-04T11:31:51.559-07:00</updated><title type='text'>Strings</title><content type='html'>A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode. See utf8_encode() and utf8_decode() for some Unicode support. &lt;br /&gt;&lt;br /&gt;????: It is no problem for a string to become very large. There is no practical bound to the size of strings imposed by PHP, so there is no reason at all to worry about long strings. &lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;A string literal can be specified in three different ways. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;single quoted &lt;br /&gt;&lt;br /&gt;double quoted &lt;br /&gt;&lt;br /&gt;heredoc syntax &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Single quoted&lt;br /&gt;The easiest way to specify a simple string is to enclose it in single quotes (the character '). &lt;br /&gt;&lt;br /&gt;To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself. &lt;br /&gt;&lt;br /&gt;????: In PHP 3, a warning will be issued at the E_NOTICE level when this happens. &lt;br /&gt;&lt;br /&gt;????: Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo 'this is a simple string';&lt;br /&gt;&lt;br /&gt;echo 'You can also have embedded newlines in &lt;br /&gt;strings this way as it is&lt;br /&gt;okay to do';&lt;br /&gt;&lt;br /&gt;// Outputs: Arnold once said: "I'll be back"&lt;br /&gt;echo 'Arnold once said: "I\'ll be back"';&lt;br /&gt;&lt;br /&gt;// Outputs: You deleted C:\*.*?&lt;br /&gt;echo 'You deleted C:\\*.*?';&lt;br /&gt;&lt;br /&gt;// Outputs: You deleted C:\*.*?&lt;br /&gt;echo 'You deleted C:\*.*?';&lt;br /&gt;&lt;br /&gt;// Outputs: This will not expand: \n a newline&lt;br /&gt;echo 'This will not expand: \n a newline';&lt;br /&gt;&lt;br /&gt;// Outputs: Variables do not $expand $either&lt;br /&gt;echo 'Variables do not $expand $either';&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Double quoted&lt;br /&gt;If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters: &lt;br /&gt;&lt;br /&gt;???? 11-1. Escaped characters&lt;br /&gt;&lt;br /&gt;sequence meaning &lt;br /&gt;\n linefeed (LF or 0x0A (10) in ASCII) &lt;br /&gt;\r carriage return (CR or 0x0D (13) in ASCII) &lt;br /&gt;\t horizontal tab (HT or 0x09 (9) in ASCII) &lt;br /&gt;\\ backslash &lt;br /&gt;\$ dollar sign &lt;br /&gt;\" double-quote &lt;br /&gt;\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation  &lt;br /&gt;\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation  &lt;br /&gt;&lt;br /&gt;Again, if you try to escape any other character, the backslash will be printed too! Before PHP 5.1.1, backslash in \{$var} hasn't been printed. &lt;br /&gt;&lt;br /&gt;But the most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details. &lt;br /&gt;&lt;br /&gt;Heredoc&lt;br /&gt;Another way to delimit strings is by using heredoc syntax ("&lt;&lt;&lt;"). One should provide an identifier after &lt;&lt;&lt;, then the string, and then the same identifier to close the quotation. &lt;br /&gt;&lt;br /&gt;The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example. Closing delimiter (possibly followed by a semicolon) must be followed by a newline too. &lt;br /&gt;&lt;br /&gt;If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script. &lt;br /&gt;&lt;br /&gt;It is not allowed to use heredoc syntax in initializing class members. Use other string syntaxes instead. ????? 11-3. Invalid example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;class foo {&lt;br /&gt;    public $bar = &lt;&lt;&lt;EOT&lt;br /&gt;bar&lt;br /&gt;EOT;&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings. ????? 11-4. Heredoc string quoting example&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$str = &lt;&lt;&lt;EOD&lt;br /&gt;Example of string&lt;br /&gt;spanning multiple lines&lt;br /&gt;using heredoc syntax.&lt;br /&gt;EOD;&lt;br /&gt;&lt;br /&gt;/* More complex example, with variables. */&lt;br /&gt;class foo&lt;br /&gt;{&lt;br /&gt;    var $foo;&lt;br /&gt;    var $bar;&lt;br /&gt;&lt;br /&gt;    function foo()&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;foo = 'Foo';&lt;br /&gt;        $this-&gt;bar = array('Bar1', 'Bar2', 'Bar3');&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$foo = new foo();&lt;br /&gt;$name = 'MyName';&lt;br /&gt;&lt;br /&gt;echo &lt;&lt;&lt;EOT&lt;br /&gt;My name is "$name". I am printing some $foo-&gt;foo.&lt;br /&gt;Now, I am printing some {$foo-&gt;bar[1]}.&lt;br /&gt;This should print a capital 'A': \x41&lt;br /&gt;EOT;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: Heredoc support was added in PHP 4. &lt;br /&gt;&lt;br /&gt;Variable parsing&lt;br /&gt;When a string is specified in double quotes or with heredoc, variables are parsed within it. &lt;br /&gt;&lt;br /&gt;There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to parse a variable, an array value, or an object property. &lt;br /&gt;&lt;br /&gt;The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. &lt;br /&gt;&lt;br /&gt;Simple syntax&lt;br /&gt;If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces if you want to explicitly specify the end of the name. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$beer = 'Heineken';&lt;br /&gt;echo "$beer's taste is great"; // works, "'" is an invalid character for varnames&lt;br /&gt;echo "He drank some $beers";   // won't work, 's' is a valid character for varnames&lt;br /&gt;echo "He drank some ${beer}s"; // works&lt;br /&gt;echo "He drank some {$beer}s"; // works&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Similarly, you can also have an array index or an object property parsed. With array indices, the closing square bracket (]) marks the end of the index. For object properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// These examples are specific to using arrays inside of strings.&lt;br /&gt;// When outside of a string, always quote your array string keys &lt;br /&gt;// and do not use {braces} when outside of strings either.&lt;br /&gt;&lt;br /&gt;// Let's show all errors&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;&lt;br /&gt;$fruits = array('strawberry' =&gt; 'red', 'banana' =&gt; 'yellow');&lt;br /&gt;&lt;br /&gt;// Works but note that this works differently outside string-quotes&lt;br /&gt;echo "A banana is $fruits[banana].";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "A banana is {$fruits['banana']}.";&lt;br /&gt;&lt;br /&gt;// Works but PHP looks for a constant named banana first&lt;br /&gt;// as described below.&lt;br /&gt;echo "A banana is {$fruits[banana]}.";&lt;br /&gt;&lt;br /&gt;// Won't work, use braces.  This results in a parse error.&lt;br /&gt;echo "A banana is $fruits['banana'].";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "A banana is " . $fruits['banana'] . ".";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This square is $square-&gt;width meters broad.";&lt;br /&gt;&lt;br /&gt;// Won't work. For a solution, see the complex syntax.&lt;br /&gt;echo "This square is $square-&gt;width00 centimeters broad.";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For anything more complex, you should use the complex syntax. &lt;br /&gt;&lt;br /&gt;Complex (curly) syntax&lt;br /&gt;This isn't called complex because the syntax is complex, but because you can include complex expressions this way. &lt;br /&gt;&lt;br /&gt;In fact, you can include any value that is in the namespace in strings with this syntax. You simply write the expression the same way as you would outside the string, and then include it in { and }. Since you can't escape '{', this syntax will only be recognised when the $ is immediately following the {. (Use "{\$" to get a literal "{$"). Some examples to make it clear: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Let's show all errors&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;&lt;br /&gt;$great = 'fantastic';&lt;br /&gt;&lt;br /&gt;// Won't work, outputs: This is { fantastic}&lt;br /&gt;echo "This is { $great}";&lt;br /&gt;&lt;br /&gt;// Works, outputs: This is fantastic&lt;br /&gt;echo "This is {$great}";&lt;br /&gt;echo "This is ${great}";&lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This square is {$square-&gt;width}00 centimeters broad."; &lt;br /&gt;&lt;br /&gt;// Works&lt;br /&gt;echo "This works: {$arr[4][3]}";&lt;br /&gt;&lt;br /&gt;// This is wrong for the same reason as $foo[bar] is wrong &lt;br /&gt;// outside a string.  In other words, it will still work but&lt;br /&gt;// because PHP first looks for a constant named foo, it will&lt;br /&gt;// throw an error of level E_NOTICE (undefined constant).&lt;br /&gt;echo "This is wrong: {$arr[foo][3]}"; &lt;br /&gt;&lt;br /&gt;// Works.  When using multi-dimensional arrays, always use&lt;br /&gt;// braces around arrays when inside of strings&lt;br /&gt;echo "This works: {$arr['foo'][3]}";&lt;br /&gt;&lt;br /&gt;// Works.&lt;br /&gt;echo "This works: " . $arr['foo'][3];&lt;br /&gt;&lt;br /&gt;echo "You can even write {$obj-&gt;values[3]-&gt;name}";&lt;br /&gt;&lt;br /&gt;echo "This is the value of the var named $name: {${$name}}";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;String access and modification by character&lt;br /&gt;Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array-brackets like $str[42] so think of a string as an array of characters. &lt;br /&gt;&lt;br /&gt;????: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred. &lt;br /&gt;&lt;br /&gt;????? 11-5. Some string examples&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Get the first character of a string&lt;br /&gt;$str = 'This is a test.';&lt;br /&gt;$first = $str[0];&lt;br /&gt;&lt;br /&gt;// Get the third character of a string&lt;br /&gt;$third = $str[2];&lt;br /&gt;&lt;br /&gt;// Get the last character of a string.&lt;br /&gt;$str = 'This is still a test.';&lt;br /&gt;$last = $str[strlen($str)-1]; &lt;br /&gt;&lt;br /&gt;// Modify the last character of a string&lt;br /&gt;$str = 'Look at the sea';&lt;br /&gt;$str[strlen($str)-1] = 'e';&lt;br /&gt;&lt;br /&gt;// Alternative method using {}&lt;br /&gt;$third = $str{2};&lt;br /&gt;&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Useful functions and operators&lt;br /&gt;Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this. Please see String operators for more information. &lt;br /&gt;&lt;br /&gt;There are a lot of useful functions for string modification. &lt;br /&gt;&lt;br /&gt;See the string functions section for general functions, the regular expression functions for advanced find&amp;replacing (in two tastes: Perl and POSIX extended). &lt;br /&gt;&lt;br /&gt;There are also functions for URL-strings, and functions to encrypt/decrypt strings (mcrypt and mhash). &lt;br /&gt;&lt;br /&gt;Finally, if you still didn't find what you're looking for, see also the character type functions. &lt;br /&gt;&lt;br /&gt;Converting to string&lt;br /&gt;You can convert a value to a string using the (string) cast, or the strval() function. String conversion is automatically done in the scope of an expression for you where a string is needed. This happens when you use the echo() or print() functions, or when you compare a variable value to a string. Reading the manual sections on Types and Type Juggling will make the following clearer. See also settype(). &lt;br /&gt;&lt;br /&gt;A boolean TRUE value is converted to the string "1", the FALSE value is represented as "" (empty string). This way you can convert back and forth between boolean and string values. &lt;br /&gt;&lt;br /&gt;An integer or a floating point number (float) is converted to a string representing the number with its digits (including the exponent part for floating point numbers). &lt;br /&gt;&lt;br /&gt;Arrays are always converted to the string "Array", so you cannot dump out the contents of an array with echo() or print() to see what is inside them. To view one element, you'd do something like echo $arr['foo']. See below for tips on dumping/viewing the entire contents. &lt;br /&gt;&lt;br /&gt;Objects are always converted to the string "Object". If you would like to print out the member variable values of an object for debugging reasons, read the paragraphs below. If you would like to find out the class name of which an object is an instance of, use get_class(). As of PHP 5, __toString() method is used if applicable. &lt;br /&gt;&lt;br /&gt;Resources are always converted to strings with the structure "Resource id #1" where 1 is the unique number of the resource assigned by PHP during runtime. If you would like to get the type of the resource, use get_resource_type(). &lt;br /&gt;&lt;br /&gt;NULL is always converted to an empty string. &lt;br /&gt;&lt;br /&gt;As you can see above, printing out the arrays, objects or resources does not provide you any useful information about the values themselves. Look at the functions print_r() and var_dump() for better ways to print out values for debugging. &lt;br /&gt;&lt;br /&gt;You can also convert PHP values to strings to store them permanently. This method is called serialization, and can be done with the function serialize(). You can also serialize PHP values to XML structures, if you have WDDX support in your PHP setup. &lt;br /&gt;&lt;br /&gt;String conversion to numbers&lt;br /&gt;When a string is evaluated as a numeric value, the resulting value and type are determined as follows. &lt;br /&gt;&lt;br /&gt;The string will evaluate as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will evaluate as an integer. &lt;br /&gt;&lt;br /&gt;The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$foo = 1 + "10.5";                // $foo is float (11.5)&lt;br /&gt;$foo = 1 + "-1.3e3";              // $foo is float (-1299)&lt;br /&gt;$foo = 1 + "bob-1.3e3";           // $foo is integer (1)&lt;br /&gt;$foo = 1 + "bob3";                // $foo is integer (1)&lt;br /&gt;$foo = 1 + "10 Small Pigs";       // $foo is integer (11)&lt;br /&gt;$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)&lt;br /&gt;$foo = "10.0 pigs " + 1;          // $foo is float (11)&lt;br /&gt;$foo = "10.0 pigs " + 1.0;        // $foo is float (11)     &lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For more information on this conversion, see the Unix manual page for strtod(3). &lt;br /&gt;&lt;br /&gt;If you would like to test any of the examples in this section, you can cut and paste the examples and insert the following line to see for yourself what's going on: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br /&gt;\n";&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Do not expect to get the code of one character by converting it to integer (as you would do in C for example). Use the functions ord() and chr() to convert between charcodes and characters. &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;????? ??? ???? ????? &lt;br /&gt;Floating point numbers ????? Arrays&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7838795723511527548?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7838795723511527548/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/strings.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7838795723511527548'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7838795723511527548'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/strings.html' title='Strings'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-1606825610939812539</id><published>2009-07-04T11:29:00.000-07:00</published><updated>2009-07-04T11:30:36.362-07:00</updated><title type='text'>Floating point numbers</title><content type='html'>Floating point numbers (AKA "floats", "doubles" or "real numbers") can be specified using any of the following syntaxes: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = 1.234; &lt;br /&gt;$b = 1.2e3; &lt;br /&gt;$c = 7E-10;&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Formally: &lt;br /&gt;&lt;br /&gt;LNUM          [0-9]+&lt;br /&gt;DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)&lt;br /&gt;EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format). &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Floating point precision &lt;br /&gt;It is quite usual that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 as the result of the internal representation really being something like 7.9999999999.... &lt;br /&gt;&lt;br /&gt;This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333. . .. &lt;br /&gt;&lt;br /&gt;So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the arbitrary precision math functions or gmp functions instead. &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Converting to float&lt;br /&gt;For information on when and how strings are converted to floats, see the section titled String conversion to numbers. For values of other types, the conversion is the same as if the value would have been converted to integer and then to float. See the Converting to integer section for more information. As of PHP 5, notice is thrown if you try to convert object to float.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-1606825610939812539?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/1606825610939812539/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/floating-point-numbers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1606825610939812539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/1606825610939812539'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/floating-point-numbers.html' title='Floating point numbers'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-7673839401822732218</id><published>2009-07-04T11:28:00.000-07:00</published><updated>2009-07-04T11:29:25.410-07:00</updated><title type='text'>Integers</title><content type='html'>Integers&lt;br /&gt;An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}. &lt;br /&gt;&lt;br /&gt;See also: Arbitrary length integer / GMP, Floating point numbers, and Arbitrary precision / BCMath &lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +). &lt;br /&gt;&lt;br /&gt;If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x. ????? 11-1. Integer literals&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a = 1234; // decimal number&lt;br /&gt;$a = -123; // a negative number&lt;br /&gt;$a = 0123; // octal number (equivalent to 83 decimal)&lt;br /&gt;$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;Formally the possible structure for integer literals is: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;decimal     : [1-9][0-9]*&lt;br /&gt;            | 0&lt;br /&gt;&lt;br /&gt;hexadecimal : 0[xX][0-9a-fA-F]+&lt;br /&gt;&lt;br /&gt;octal       : 0[0-7]+&lt;br /&gt;&lt;br /&gt;integer     : [+-]?decimal&lt;br /&gt;            | [+-]?hexadecimal&lt;br /&gt;            | [+-]?octal&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;If an invalid digit is passed to octal integer (i.e. 8 or 9), the rest of the number is ignored. ????? 11-2. Octal weirdness&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;var_dump(01090); // 010 octal = 8 decimal&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Integer overflow&lt;br /&gt;If you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, if you perform an operation that results in a number beyond the bounds of the integer type, a float will be returned instead. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$large_number =  2147483647;&lt;br /&gt;var_dump($large_number);&lt;br /&gt;// output: int(2147483647)&lt;br /&gt;&lt;br /&gt;$large_number =  2147483648;&lt;br /&gt;var_dump($large_number);&lt;br /&gt;// output: float(2147483648)&lt;br /&gt;&lt;br /&gt;// it's true also for hexadecimal specified integers between 2^31 and 2^32-1:&lt;br /&gt;var_dump( 0xffffffff );&lt;br /&gt;// output: float(4294967295)&lt;br /&gt;&lt;br /&gt;// this doesn't go for hexadecimal specified integers above 2^32-1:&lt;br /&gt;var_dump( 0x100000000 );&lt;br /&gt;// output: int(2147483647)&lt;br /&gt;&lt;br /&gt;$million = 1000000;&lt;br /&gt;$large_number =  50000 * $million;&lt;br /&gt;var_dump($large_number);&lt;br /&gt;// output: float(50000000000)&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;Unfortunately, there was a bug in PHP so that this does not always work correctly when there are negative numbers involved. For example: when you do -50000 * $million, the result will be -429496728. However, when both operands are positive there is no problem. &lt;br /&gt;&lt;br /&gt;This is solved in PHP 4.1.0. &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is no integer division operator in PHP. 1/2 yields the float 0.5. You can cast the value to an integer to always round it downwards, or you can use the round() function. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;var_dump(25/7);         // float(3.5714285714286) &lt;br /&gt;var_dump((int) (25/7)); // int(3)&lt;br /&gt;var_dump(round(25/7));  // float(4) &lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Converting to integer&lt;br /&gt;To explicitly convert a value to integer, use either the (int) or the (integer) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires an integer argument. You can also convert a value to integer with the function intval(). &lt;br /&gt;&lt;br /&gt;See also type-juggling. &lt;br /&gt;&lt;br /&gt;From booleans&lt;br /&gt;FALSE will yield 0 (zero), and TRUE will yield 1 (one). &lt;br /&gt;&lt;br /&gt;From floating point numbers&lt;br /&gt;When converting from float to integer, the number will be rounded towards zero. &lt;br /&gt;&lt;br /&gt;If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), the result is undefined, since the float hasn't got enough precision to give an exact integer result. No warning, not even a notice will be issued in this case! &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;echo (int) ( (0.1+0.7) * 10 ); // echoes 7!&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;See for more information the warning about float-precision.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-7673839401822732218?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/7673839401822732218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/integers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7673839401822732218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/7673839401822732218'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/integers.html' title='Integers'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-3280996412371444298</id><published>2009-07-04T11:25:00.001-07:00</published><updated>2009-07-04T11:25:32.391-07:00</updated><title type='text'>Booleans</title><content type='html'>This is the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE. &lt;br /&gt;&lt;br /&gt;????: The boolean type was introduced in PHP 4. &lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;To specify a boolean literal, use either the keyword TRUE or FALSE. Both are case-insensitive. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$foo = True; // assign the value TRUE to $foo&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Usually you use some kind of operator which returns a boolean value, and then pass it on to a control structure. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// == is an operator which test&lt;br /&gt;// equality and returns a boolean&lt;br /&gt;if ($action == "show_version") {&lt;br /&gt;    echo "The version is 1.23";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// this is not necessary...&lt;br /&gt;if ($show_separators == TRUE) {&lt;br /&gt;    echo "&lt;hr&gt;\n";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// ...because you can simply type&lt;br /&gt;if ($show_separators) {&lt;br /&gt;    echo "&lt;hr&gt;\n";&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Converting to boolean&lt;br /&gt;To explicitly convert a value to boolean, use either the (bool) or the (boolean) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires a boolean argument. &lt;br /&gt;&lt;br /&gt;See also Type Juggling. &lt;br /&gt;&lt;br /&gt;When converting to boolean, the following values are considered FALSE: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;the boolean FALSE itself&lt;br /&gt;&lt;br /&gt;the integer 0 (zero) &lt;br /&gt;&lt;br /&gt;the float 0.0 (zero) &lt;br /&gt;&lt;br /&gt;the empty string, and the string "0"&lt;br /&gt;&lt;br /&gt;an array with zero elements&lt;br /&gt;&lt;br /&gt;an object with zero member variables (PHP 4 only)&lt;br /&gt;&lt;br /&gt;the special type NULL (including unset variables) &lt;br /&gt;&lt;br /&gt;SimpleXML objects created from empty tags &lt;br /&gt;&lt;br /&gt;Every other value is considered TRUE (including any resource). &lt;br /&gt;&lt;br /&gt;????? &lt;br /&gt;-1 is considered TRUE, like any other non-zero (whether negative or positive) number! &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;var_dump((bool) "");        // bool(false)&lt;br /&gt;var_dump((bool) 1);         // bool(true)&lt;br /&gt;var_dump((bool) -2);        // bool(true)&lt;br /&gt;var_dump((bool) "foo");     // bool(true)&lt;br /&gt;var_dump((bool) 2.3e5);     // bool(true)&lt;br /&gt;var_dump((bool) array(12)); // bool(true)&lt;br /&gt;var_dump((bool) array());   // bool(false)&lt;br /&gt;var_dump((bool) "false");   // bool(true)&lt;br /&gt;?&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-3280996412371444298?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/3280996412371444298/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/booleans.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3280996412371444298'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/3280996412371444298'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/booleans.html' title='Booleans'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5191205108435930606</id><published>2009-07-04T11:22:00.001-07:00</published><updated>2009-07-04T11:22:48.842-07:00</updated><title type='text'>PHP supports eight primitive types.</title><content type='html'>Four scalar types: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;boolean &lt;br /&gt;&lt;br /&gt;integer &lt;br /&gt;&lt;br /&gt;float (floating-point number, aka 'double') &lt;br /&gt;&lt;br /&gt;string &lt;br /&gt;&lt;br /&gt;Two compound types: &lt;br /&gt;&lt;br /&gt;array &lt;br /&gt;&lt;br /&gt;object &lt;br /&gt;&lt;br /&gt;And finally two special types: &lt;br /&gt;&lt;br /&gt;resource &lt;br /&gt;&lt;br /&gt;NULL &lt;br /&gt;&lt;br /&gt;This manual also introduces some pseudo-types for readability reasons: &lt;br /&gt;&lt;br /&gt;mixed &lt;br /&gt;&lt;br /&gt;number &lt;br /&gt;&lt;br /&gt;callback &lt;br /&gt;&lt;br /&gt;You may also find some references to the type "double". Consider double the same as float, the two names exist only for historic reasons. &lt;br /&gt;&lt;br /&gt;The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used. &lt;br /&gt;&lt;br /&gt;????: If you want to check out the type and value of a certain expression, use var_dump(). &lt;br /&gt;&lt;br /&gt;????: If you simply want a human-readable representation of the type for debugging, use gettype(). To check for a certain type, do not use gettype(), but use the is_type functions. Some examples: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$a_bool = TRUE;   // a boolean&lt;br /&gt;$a_str  = "foo";  // a string&lt;br /&gt;$a_str2 = 'foo';  // a string&lt;br /&gt;$an_int = 12;     // an integer&lt;br /&gt;&lt;br /&gt;echo gettype($a_bool); // prints out:  boolean&lt;br /&gt;echo gettype($a_str);  // prints out:  string&lt;br /&gt;&lt;br /&gt;// If this is an integer, increment it by four&lt;br /&gt;if (is_int($an_int)) {&lt;br /&gt;    $an_int += 4;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// If $bool is a string, print it out&lt;br /&gt;// (does not print out anything)&lt;br /&gt;if (is_string($a_bool)) {&lt;br /&gt;    echo "String: $a_bool";&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you would like to force a variable to be converted to a certain type, you may either cast the variable or use the settype() function on it. &lt;br /&gt;&lt;br /&gt;Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on Type Juggling. Also, you may be interested in viewing the type comparison tables, as they show examples of various type related comparisons.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5191205108435930606?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5191205108435930606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/php-supports-eight-primitive-types.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5191205108435930606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5191205108435930606'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/php-supports-eight-primitive-types.html' title='PHP supports eight primitive types.'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-6315074992593180652</id><published>2009-07-04T11:09:00.001-07:00</published><updated>2009-07-04T11:09:41.425-07:00</updated><title type='text'>Escaping from HTML</title><content type='html'>When PHP parses a file, it looks for opening and closing tags, which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows php to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. Most of the time you will see php embedded in HTML documents, as in this example. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is going to be ignored.&lt;/p&gt;&lt;br /&gt;&lt;?php echo 'While this is going to be parsed.'; ?&gt;&lt;br /&gt;&lt;p&gt;This will also be ignored.&lt;/p&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can also use more advanced structures: ????? 10-1. Advanced escaping&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;if ($expression) { &lt;br /&gt;    ?&gt;&lt;br /&gt;    &lt;strong&gt;This is true.&lt;/strong&gt;&lt;br /&gt;    &lt;?php &lt;br /&gt;} else { &lt;br /&gt;    ?&gt;&lt;br /&gt;    &lt;strong&gt;This is false.&lt;/strong&gt;&lt;br /&gt;    &lt;?php &lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt; &lt;br /&gt;This works as expected, because when PHP hits the ?&gt; closing tags, it simply starts outputting whatever it finds until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print(). &lt;br /&gt;&lt;br /&gt;There are four different pairs of opening and closing tags which can be used in php. Two of those, &lt;?php ?&gt; and &lt;script language="php"&gt; &lt;/script&gt;, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended. &lt;br /&gt;&lt;br /&gt;????: Also note that if you are embedding PHP within XML or XHTML you will need to use the &lt;?php ?&gt; tags to remain compliant with standards. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????? 10-2. PHP Opening and Closing Tags&lt;br /&gt;&lt;br /&gt;1.  &lt;?php echo 'if you want to serve XHTML or XML documents, do like this'; ?&gt;&lt;br /&gt;&lt;br /&gt;2.  &lt;script language="php"&gt;&lt;br /&gt;        echo 'some editors (like FrontPage) don\'t&lt;br /&gt;              like processing instructions';&lt;br /&gt;    &lt;/script&gt;&lt;br /&gt;&lt;br /&gt;3.  &lt;? echo 'this is the simplest, an SGML processing instruction'; ?&gt;&lt;br /&gt;    &lt;?= expression ?&gt; This is a shortcut for "&lt;? echo expression ?&gt;"&lt;br /&gt;&lt;br /&gt;4.  &lt;% echo 'You may optionally use ASP-style tags'; %&gt;&lt;br /&gt;    &lt;%= $variable; # This is a shortcut for "&lt;% echo . . ." %&gt;  &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two. &lt;br /&gt;&lt;br /&gt;Short tags (example three) are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if php was configured with the --enable-short-tags option. &lt;br /&gt;&lt;br /&gt;????: If you are using PHP 3 you may also enable short tags via the short_tags() function. This is only available in PHP 3! &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ASP style tags (example four) are only available when they are enabled via the asp_tags php.ini configuration file directive. &lt;br /&gt;&lt;br /&gt;????: Support for ASP tags was added in 3.0.4. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;????: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags. &lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-6315074992593180652?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/6315074992593180652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/escaping-from-html.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6315074992593180652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/6315074992593180652'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/escaping-from-html.html' title='Escaping from HTML'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-9084736296974903580</id><published>2009-07-04T10:55:00.001-07:00</published><updated>2009-07-04T10:55:43.075-07:00</updated><title type='text'>Dealing with Forms</title><content type='html'>One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here is an example HTML form: &lt;br /&gt;&lt;br /&gt;????? 2-6. A simple HTML form&lt;br /&gt;&lt;br /&gt;&lt;form action="action.php" method="post"&gt;&lt;br /&gt; &lt;p&gt;Your name: &lt;input type="text" name="name" /&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;Your age: &lt;input type="text" name="age" /&gt;&lt;/p&gt;&lt;br /&gt; &lt;p&gt;&lt;input type="submit" /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called. In this file you would write something like this: &lt;br /&gt;&lt;br /&gt;????? 2-7. Printing data from our form&lt;br /&gt;&lt;br /&gt;Hi &lt;?php echo $_POST['name']; ?&gt;.&lt;br /&gt;You are &lt;?php echo $_POST['age']; ?&gt; years old.  &lt;br /&gt;&lt;br /&gt;A sample output of this script may be: &lt;br /&gt;&lt;br /&gt;Hi Joe. You are 22 years old.&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It should be obvious what this does. There is nothing more to it. The $_POST['name'] and $_POST['age'] variables are automatically set for you by PHP. Earlier we used the $_SERVER autoglobal; above we just introduced the $_POST autoglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST autoglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data. Also see the import_request_variables() function. &lt;br /&gt;&lt;br /&gt;You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them. We also have a short introduction to handling data received from XForms in our features section.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-9084736296974903580?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/9084736296974903580/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/dealing-with-forms.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/9084736296974903580'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/9084736296974903580'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/dealing-with-forms.html' title='Dealing with Forms'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-5917089617927378239</id><published>2009-07-04T10:45:00.000-07:00</published><updated>2009-07-04T10:47:39.075-07:00</updated><title type='text'>Something Useful</title><content type='html'>Something Useful&lt;br /&gt;Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT']. &lt;br /&gt;&lt;br /&gt;????: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). See the related manual page on superglobals for more information. These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist. (See also the note on old code.) &lt;br /&gt;&lt;br /&gt;To display this variable, you can simply do: &lt;br /&gt;&lt;br /&gt;????? 2-3. Printing a variable (Array element)&lt;br /&gt;&lt;br /&gt;&lt;?php echo $_SERVER['HTTP_USER_AGENT']; ?&gt;  &lt;br /&gt;&lt;br /&gt;A sample output of this script may be: &lt;br /&gt;&lt;br /&gt;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful. &lt;br /&gt;&lt;br /&gt;$_SERVER is just one variable that PHP automatically makes available to you. A list can be seen in the Reserved Variables section of the manual or you can get a complete list of them by looking at the output of the phpinfo() function used in the example in the previous section. &lt;br /&gt;&lt;br /&gt;You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this: &lt;br /&gt;&lt;br /&gt;????? 2-4. Example using control structures and functions&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {&lt;br /&gt;    echo 'You are using Internet Explorer.&lt;br /&gt;';&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;A sample output of this script may be: &lt;br /&gt;&lt;br /&gt;You are using Internet Explorer.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up an introductory PHP book and read the first couple of chapters, or read the Language Reference part of the manual. You can find a list of PHP books at http://www.php.net/books.php. &lt;br /&gt;&lt;br /&gt;The second concept we introduced was the strpos() function call. strpos() is a function built into PHP which searches a string for another string. In this case we are looking for 'MSIE' (so-called needle) inside $_SERVER['HTTP_USER_AGENT'] (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns FALSE. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions such as strtoupper() and strlen(). Each related manual page contains examples too. If you are unsure how to use functions, you will want to read both the manual page on how to read a function definition and the section about PHP functions. &lt;br /&gt;&lt;br /&gt;We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block: &lt;br /&gt;&lt;br /&gt;????? 2-5. Mixing both HTML and PHP modes&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {&lt;br /&gt;?&gt;&lt;br /&gt;&lt;h3&gt;strpos() must have returned non-false&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;You are using Internet Explorer&lt;/p&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;} else {&lt;br /&gt;?&gt;&lt;br /&gt;&lt;h3&gt;strpos() must have returned false&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;You are not using Internet Explorer&lt;/p&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;}&lt;br /&gt;?&gt;  &lt;br /&gt;&lt;br /&gt;A sample output of this script may be: &lt;br /&gt;&lt;br /&gt;&lt;h3&gt;strpos() must have returned non-false&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;You are using Internet Explorer&lt;/p&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos(). In other words, it depends on whether the string MSIE was found or not&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-5917089617927378239?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/5917089617927378239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/something-useful.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5917089617927378239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/5917089617927378239'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/something-useful.html' title='Something Useful'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-4726519350239760193</id><published>2009-07-04T10:42:00.000-07:00</published><updated>2009-07-04T10:44:05.886-07:00</updated><title type='text'>Your first PHP-enabled page</title><content type='html'>Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content: &lt;br /&gt;&lt;br /&gt;????? 2-1. Our first PHP script: hello.php&lt;br /&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt; &lt;head&gt;&lt;br /&gt;  &lt;title&gt;PHP Test&lt;/title&gt;&lt;br /&gt; &lt;/head&gt;&lt;br /&gt; &lt;body&gt;&lt;br /&gt; &lt;?php echo '&lt;p&gt;Hello World&lt;/p&gt;'; ?&gt; &lt;br /&gt; &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;  &lt;br /&gt;&lt;br /&gt;Use your browser to access the file with your web server's URL, ending with the "/hello.php" file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server's configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser: &lt;br /&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt; &lt;head&gt;&lt;br /&gt;  &lt;title&gt;PHP Test&lt;/title&gt;&lt;br /&gt; &lt;/head&gt;&lt;br /&gt; &lt;body&gt;&lt;br /&gt; &lt;p&gt;Hello World&lt;/p&gt;&lt;br /&gt; &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo() statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the ".php" extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things. &lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-4726519350239760193?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/4726519350239760193/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/your-first-php-enabled-page.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4726519350239760193'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/4726519350239760193'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/your-first-php-enabled-page.html' title='Your first PHP-enabled page'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7781560989272961089.post-2337137649073621429</id><published>2009-07-04T10:33:00.000-07:00</published><updated>2009-07-04T10:41:01.285-07:00</updated><title type='text'>what is php programming</title><content type='html'>&lt;p&gt;&lt;acronym class="acronym"&gt;PHP&lt;/acronym&gt; (recursive acronym for "PHP: Hypertext  Preprocessor") is a widely-used Open Source general-purpose scripting language  that is especially suited for Web development and can be embedded into HTML.  &lt;/p&gt; &lt;p&gt;Simple answer, but what does that mean? An example: &lt;/p&gt; &lt;p&gt; &lt;table class="EXAMPLE" border="0" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;div class="example"&gt;&lt;a name="AEN83"&gt;&lt;/a&gt; &lt;p&gt;&lt;b&gt;????? 1-1. An introductory example&lt;/b&gt;&lt;/p&gt; &lt;table bgcolor="#e0e0e0" border="0" cellpadding="5"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;html&gt;&lt;br /&gt;    &lt;head&gt;&lt;br /&gt;        &lt;title&gt;Example&lt;/title&gt;&lt;br /&gt;    &lt;/head&gt;&lt;br /&gt;    &lt;body&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Hi, I'm a PHP script!"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;    &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;/span&gt;  &lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt; &lt;p&gt;Notice how this is different from a script written in other languages like  Perl or C -- instead of writing a program with lots of commands to output HTML,  you write an HTML script with some embedded code to do something (in this case,  output some text). The PHP code is enclosed in special &lt;a href="language.basic-syntax.html#language.basic-syntax.phpmode"&gt;start and end  tags&lt;/a&gt; that allow you to jump into and out of "PHP mode". &lt;/p&gt; &lt;p&gt;What distinguishes PHP from something like client-side JavaScript is that the  code is executed on the server. If you were to have a script similar to the  above on your server, the client would receive the results of running that  script, with no way of determining what the underlying code may be. You can even  configure your web server to process all your HTML files with PHP, and then  there's really no way that users can tell what you have up your sleeve. &lt;/p&gt; &lt;p&gt;The best things in using PHP are that it is extremely simple for a newcomer,  but offers many advanced features for a professional programmer. Don't be afraid  reading the long list of PHP's features. You can jump in, in a short time, and  start writing simple scripts in a few hours. &lt;/p&gt; &lt;p&gt;Although PHP's development is focused on server-side scripting, you can do  much more with it. Read on, and see more in the &lt;a href="intro-whatcando.html"&gt;What can PHP do?&lt;/a&gt; section, or go right to the &lt;a href="tutorial.html"&gt;introductory tutorial&lt;/a&gt; if you are only interested in web  programming. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7781560989272961089-2337137649073621429?l=programmingandphp.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingandphp.blogspot.com/feeds/2337137649073621429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/what-is-php-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2337137649073621429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7781560989272961089/posts/default/2337137649073621429'/><link rel='alternate' type='text/html' href='http://programmingandphp.blogspot.com/2009/07/what-is-php-programming.html' title='what is php programming'/><author><name>LikeStupid</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
