PHP

/PHP

Custom Sort Functions – Organizing PHP Arrays

First I am going to demonstrate the use of the php function usort(), and then show an example of how to organize your sorting features. There isn’t much to know about the function but it’s good to practice using it since sorting functions can get pretty complex when needed. //A function must be defined and it must have 2 arguments function sort_by_suffix($a, $b){ $ax = intval(substr($a, 5)); $bx = intval(substr($b, 5)); if ($ax == $bx) return 0; elseif($a < $b) return -1; else return 1; } //an array of made up codes with a suffix at the end of each one $codes = array("G45G-5", "G15G-2", "G11G-1", "G33G-3"); //usort modifies the array rather than returning it. usort($codes, sort_by_suffix); That was an example of sorting by a portion of a string. Whatever part of the string after 5 characters that could be made into a number was used as a sortable value. If it couldn’t find sortable values the order would be unpredictable. If the suffixes were all the same it would also be that way. […]

By | October 8th, 2011|PHP, PHP, PHP Articles|Comments Off on Custom Sort Functions – Organizing PHP Arrays

PHP Regular Expression Techniques – Part 2: Characters

Now we turn to a component of regular expressions that cannot be left out. We will be dealing with characters, how to use them, and how to escape them when needed. Regular expressions are explained in the PHP manual, and in this article we are focusing on the type that are Perl compatible, like in the function preg_match. The pattern is the regular expression we are using in each case. Since it uses a slash at the beginning and end, here’s the first thing to understand about characters in patterns: each character has a meaning or function, and escaping the character gives it a different meaning. preg_replace("/catalog/toys/", "catalog/fun", "http://shop.com/catalog/toys"); //this basically cannot work because of the 2nd slash preg_replace("/catalog/toys/", "catalog/fun", "http://shop.com/catalog/toys"); //this is correct Escaping a character, as shown, is usually butting the backslash character before it. It is also normal to put it in front of a double quote or another backslash in a PHP string like that anyway, but in patterns we use it for a various things. preg_replace("/[rn]/", "", "http://shop.com/catalog/toysn"); //removes the newline r and n are also standard php string characters representing newlines. (There may be different systems for newlines from different operating systems and programs, some use n only, some use rn.) The [] brackets in the pattern mean you want to match any character inside the brackets. The function basically means replace r or n with “”. If you wanted to actually match brackets instead of that you can escape them too: [] […]

By | October 8th, 2011|PHP, PHP, PHP Articles, PHP Tutorials|Comments Off on PHP Regular Expression Techniques – Part 2: Characters

Tips for OOP Programming with PHP- Part 2: Object Storage

Before starting this article/tutorial you should already be familiar with PHP, and basic class and object syntax. This article is going to go through some useful operations you can do with objects. The first thing I want to demonstrate is how to store objects and their member variables. class MyCart{ var $items; function add_item ($artnr, $num){ $this->items[$artnr] += $num; } } session_start(); $_SESSION[‘user_cart’] = new MyCart(); In this case we are storing the object in the session variable. It is a fairly painless process, and getting that stored object on the next page the user loads is just as easy, after session_start() it will be available just like a session variable. If you try to use the session stored object on a page that does not have the class’s declaration, class MyCart{}, you will get a generic object with no member functions, as explained on the PHP manual page ‘Object Serialization’. $user_cart = new MyCart(); $user_cart_string_data = serialize($user_cart); $fp = fopen(‘data.txt’, ‘w’); fwrite($fp, $user_cart_string_data); fclose($fp); This is another basic method of storing objects. We converted the object to a string representation, and made or overwrote a file with that data. If it is not already clear, you restore the object by reading the file into a string, then using unserialize on it, reversing the effects of serialize. Like the session type of storage, you need the class to be defined in any file that uses that type of object. […]

By | October 8th, 2011|PHP, PHP, PHP Articles, PHP Tutorials|Comments Off on Tips for OOP Programming with PHP- Part 2: Object Storage

PHP Regular Expression Techniques – Part 1: Pattern Modifiers

This article will focus on PHP’s regular expression functions with pattern modifiers. Specifically these functions are of the preg type, and have preg as the prefix. preg_match() is probably the most basic regular expression function of that type and some examples of it will be used in this article. Regular expressions provide versatile ways of searching text or ‘matching’ it. A string called a pattern is used, similar to a search phrase, and in this case the patterns take a form that is basically the same a the expressions in the Perl language. […]

By | October 4th, 2011|PHP, PHP, PHP Articles, PHP Tutorials|Comments Off on PHP Regular Expression Techniques – Part 1: Pattern Modifiers

Tips for OOP Programming with PHP- Part 1: Objects and References

Programming in PHP utilizing objects is not complex and the syntax and other basics are well covered in various articles and tutorials. It’s also covered in the PHP manual and I will include a brief example for those who need a refresher. Before that I am going to illustrate the basic principals of references without using objects at all. I will then build upon that example to show how use objects and references in your program in useful and efficient ways. […]

By | October 3rd, 2011|PHP, PHP, PHP Articles, PHP Tutorials|Comments Off on Tips for OOP Programming with PHP- Part 1: Objects and References

strlen and count in PHP5

strlen and count in PHP5 – The Basics If you are new to strlen and/or count in PHP, then you have come to the right place. In this article I will cover the basics you need to understand their uses, and move toward effectively implementing them in your own PHP projects. Both functions are used to count things, however what they count, and how they count them, are completely different. Understanding how they each work by themselves will allow you to use them together to accomplish some very useful things. However, for now, let’s start at the very beginning. […]

By | May 17th, 2011|PHP, PHP, PHP Articles|Comments Off on strlen and count in PHP5

PHP and cURL

Using curl with PHP is incredibly easy. Firstly you’ll need to make sure that you have the PHP curl library installed on your system. On Debian, this is as easy as apt-get install php5-curl Now you can try the following: […]

By | May 17th, 2011|PHP, PHP, PHP Articles|0 Comments

Web Design in India

Need Web Design in India? Contact us today and get your job done! More and more web design is being outsourced to India – why? 3 Reasons to outsource your web design to India Great talent available at a fraction of regular rates Large pool of talent to choose from Access resources not internally available 3 Reasons NOT to outsource your web design to India Communication barriers Mismatched expectations and delays Poor quality of work: short term savings – long term cost Outsourcing can be a minefield, and that’s why we’re here. Our offshore developers allow us to offer massive cost savings whilst our management team provide the boutique level responsive service and range of expertise that your project deserves. […]

By | August 19th, 2010|PHP, PHP Developer, Web Development|0 Comments

CMS Development

What is a CMS? A CMS is a Content Management System. WordPress, Joomla, Drupal and osCommerce are 4 popular PHP content management systems that we work with. A CMS at minimum provides you with a user friendly means of managing your site and it’s content. CMS Development Depending on the requirements for your CMS development project, there are two directions to consider: Take an existing CMS, and build on it to meet your needs. Taking WordPress as an example, the possibilities and options for modification are infinite. This is achieved through the development of themes and plugins. There is really no limit to the customization that’s achievable with most popular CMSs and so bespoke CMS development is usually reserved for a project where the majority of functionality would need to be manually built irrespective of existing code and modules. If your needs aren’t reasonably met by an existing CMS, then it may be more time and cost effective to build a CMS from the ground up. With this route, you also get to put exactly what features you want, where you want. We have a base framework that we usually build start our CMS development with. It contains the basics for rich page and text creation, article creation and SEO friendly URL management. In reality, the term ‘CMS’ has been stretched to cover frameworks and beyond. Whilst strictly speaking a CMS is any system that allows content to be managed, the term also describes fully fledged online site management and development platforms. […]