background preloader

PHP Variable and Array Tests

PHP Variable and Array Tests

Deciphering Magic Methods in PHP PHP provides a number of 'magic' methods that allow you to do some pretty neat tricks in object oriented programming. These methods, identified by a two underscore prefix (__), function as interceptors that are automatically called when certain conditions are met. Magic methods provide some extremely useful functionality, and this tutorial will demonstrate each method's use. Before We Begin In order to fully understand magic methods, it's helpful to see them in action. If words like "method" and "property" sound alien to you, you might want to read up on this first. Device objects will hold a name, a Battery object, an array of data, and a handle to some external resource. This tutorial assumes you have a basic understanding of object oriented programming. These classes are pretty useless, but they make a good example for each of the magic methods. Constructors & Destructors Constructors and destructors are called when an object is created and destroyed, respectively. __construct() __get()

Object Oriented PHP Tutorial for Beginners Preamble The hardest thing to learn (and teach btw,) in object oriented PHP is the basics. But once you understand them, the rest will come much, much easier. But don't be discouraged! … Or so I've been told. About This Tutorial This tutorial is designed to teach total beginners object oriented PHP. This is not a long-winded theoretical blathering that you see all too often; instead, we actually start writing OO code very quickly. So before you begin, get out your favorite PHP code editor and be ready to write and run some object oriented PHP code. PS: scroll to the bottom of the page for the supporting videos. Thanks for reading,Stefan Mischook Introduction With the release of php5 in 2004, php programmers finally had the power to code with the 'big boys'. In this tutorial, you will be guided (step-by-step) through the process of building and working with objects using php's built-in OOP capabilities. OOP Videos

How to allow users to download files in PHP So, you want to allow your users to download files from your server. Downloading files in php is a simple process.You do not need scores of lines in your code to achieve that.3 steps and you are done. A working example for the same can be seen here. Steps: 1- Specify content type – this will tell the browser which type of file do you wish to send to user’s browser. 2- Specify the name of the file for user and show an open/save dialog box. 3- Read the original file from your server and render it to users browser. Here is the php code: //download.php //content type header('Content-type: text/plain'); //open/save dialog box header('Content-Disposition: attachment; filename="sample.txt"'); //read from server and write to buffer readfile('test.txt'); Yes, thats all. First we specified a content type using the header function.header is used to send http headers to the browser.Content type will be set depending on the file type to be downloaded. ///index.html Click to download Thats all.

PHP snippets to interact with Twitter Get number of Twitter followers Have you seen my blog sidebar? I display the number of followers I have in full text. Once you have the function, you can call it as shown below: $nb = get_followers('phpsnippets'); echo "PHP Snippets already have ". » Credit: Get latest Twitter status Using PHP and cURL, it is pretty easy to get the status of a specific user. The function is extremely easy to use: echo get_status('catswhocode'); » Link to update status, without encoding problems Many websites and blogs show you how to create a link to Twitter that will update your status. <a href=" So, where’s the change? A working example can be seen on my company website: Get number of retweets for a specific page Most bloggers are using the Tweetmeme widget to display the number of retweets of their posts. Testing friendship between two users

37 Tested PHP, Perl, and JavaScript Regular Expressions inShare22 A regular expression, also called regex or regexp for short, is simply a piece of code that matches a pattern. Mastering regular expressions can be a difficult chore, and if you don't need them all of the time, the syntax is tricky enough to make the task frustrating or slow as you will constantly need to use a reference sheet. In order to save you time, I've compiled a list of PHP, Perl, and JavaScript regular expressions for common use cases that have been tested and are ready to go. This isn't a regular expression tutorial or even a reference; you can think of it more as a cheatsheet for when you just need the regex but don't want to put a lot of time into relearning regular expressions. If you're looking for regex tutorials or regex resources, you can find them at the end of the page as well as some additional regex resources. Perl and PHP Regular Expressions All Major Credit Cards Alpha-Numeric Characters Test for alpha-numeric characters with this regexp. Alphabetic Characters

How to create chained select with PHP and jQuery? In this article we’ll talk about the chaining of select also called select cascade. It is a procedure that we often find on the web and it consists in giving the user the possibility to find a specific data through a few passages: practically choosing a category in the first select, the second one will be populated with the results related with the category selected. We imagine, for example, a first select where it is possible to choose between motorcycles and cars; once we selected the car category , the second select will be populated with the brands of the cars available. Then selecting a brand, the third select will be populated with the models available of this brand. In this article we’ll see a very simple example but it will give us the basis to comprehend the procedure to do even in the case of more complicated cascades. You can examine what we will create in this page of example. As you can see, the example is very minimalist. Create the structure of the database id_catname

PHP: Hypertext Preprocessor How to Create an Infinite Scrolling Web Gallery When working my way through a web gallery, I find it annoying when I must change pages; so in today's tutorial, we will learn how to create an auto-generating, one-page, infinite scrolling gallery with PHP and AJAX. Let's get started! Step 1: Project Outline We'll begin with a normal gallery page that has a container for our images, and we'll monitor the scroll position by calling a JavaScript function at a quick interval. Step 2: HTML Markup We'll work with a very basic setup: a header and the container for our images. Step 3: CSS The CSS is also quite basic. Step 4 Then, for the container and images, I used a bit of CSS3 to add round corners and shadows. Step 5: PHP Script This is going to be very short. We define a variable for the directory we want to get the image names from, test if it exists, and if we can open it, read all the file names from it. Step 6 Now we have to build our response text. As a result, we get the remainder of the division between these two elements. Step 7 Step 9

Automatic Face Detection in Photos with PHP I have always wondered how to detect faces automatically with php script. I have seen in many photo sharing and social network sites automatically detect a face and tag a name after being uploaded. In this article, i will explain how possible this task can be achieved with simplicity with OpenCV and PHP Facedetect extension. Goal To auto detect faces in a photo and draw pink box around the faces with a php script running a linux centos server. Requirements - Linux server running Centos with SSH access - PHP/Apache - GD Library - OpenCV [Download] - PHP Facedetect extension [Download] PHP facedetect extension is very simple. Installation We install opencv and then we compile the php facedetect extension with php. How to Install OpenCV If you are running centos then one single line will install opencv. yum install opencv OpenCV may also need the following depencies to work properly and you will need to install them as well. For more installation instructions on linux see here How to test run OpenCV

'MySQLi' for Beginners Introduction Nearly every site that you visit nowadays has some sort of database storage in place, many sites opt to use MySQL databases when using PHP. However, many people haven't yet taken the step to interacting with databases properly in PHP. Here we guide you through what you should be doing - using PHP's MySQLi class - with a hat-tip to the one way that you definitely shouldn't be doing it. The Wrong Way If you're using a function called mysql_connect() or mysql_query() you really need to take note and change what you're doing. Any of the functions that are prefixed with mysql_ are now being discouraged by PHP themselves as visible on this doc page, instead you should look to use one of the following: Each has its advantages, PDO for example will work with various different database systems, where as MySQLi will only work with MySQL databases. PHP MySQLi Connecting Obviously, the database name is optional and can be omitted. Querying Output query results Number of returned rows <? <?

How to Upload files with PHP » Martin Gardner.co.uk In this Tutorial I’m going to outline the process of how you can use a simple PHP script to upload files directly to your server, re-naming them and storing them in a chosen location, all from a very simple form which you can style and place anywhere on your webpages… things to keep in mind for this script to function properly you will require the following: Web hostingPHP 5 or above installedWrite access to a directory on your web serversome PHP knowledge ( to allow for your own customization)basic CSS & Colour values to style your form Before we get to any code you’re going to need a HTML form, in order to pass information to the script, like the name of the file, size, file extension etc… but for this script we will only have to deal with the name of the file. below is an html form with some basic CSS styles applied purely for aesthetic reasons of my own. The PHP The CSS The First part of the code initialises the first two variables and assigns them with no value.

Qcodo Development Framework - Home Build seven good object-oriented habits in PHP Make your PHP applications better with object orientation Nathan GoodPublished on October 28, 2008 In the early days of PHP programming, PHP code was limited to being procedural in nature. Procedural code is characterized by the use of procedures for the building blocks of the application. Procedures offer a certain level of reuse by allowing procedures to be called by other procedures. However, without object-oriented language constructs, a programmer can still introduce OO characteristics into PHP code. While purely procedural designs without much modularity run just fine, the advantages of OO design show up in the maintenance. Modularity— one of the key characteristics of good OO design — helps with this maintenance. While there are more than seven habits to building OO software overall, the seven habits here are what you need to make your code fit basic OO design criteria. The seven good PHP OO habits are: Be modest Bad habit: Expose public fields Listing 1. Listing 2. Listing 3.

Related: