PHP Shorthand If Notation or the Ternary Operator

Why should I care about PHP Shorthand If Notation or the Ternary Operator

Programming would be a bit useless without being able to evaluate conditions using if/end and switch statements. If/Else statements is obviously a great tool, but they aren’t optimal (or necessary) in all situations. And so I introduce you to … PHP Shorthand If Notation or the Ternary Operator.

Ternary operator takes the form “(condition) ? (true return value) : (false return value)” to shorten your if/else structures.
Continue reading “PHP Shorthand If Notation or the Ternary Operator”

Hints to help optimise the performance of a PHP website (LAMP)

Over time, the websites I have been involved in have became more and more complex (and indeed carry more and more traffic).

And so the ever increasing need to optimise and increase the performance of your website (or web-application) becomes more crucial.

As you will appreciate this is a HUGE subject, so I aim to simply put you on the path to devising a better architecture and design. But I will attempt to introduce some ideas to assist…

Continue reading “Hints to help optimise the performance of a PHP website (LAMP)”

Working with Dates & Timestamps in PHP

Working with Dates and Timestamps is always trying, but using UNIX timestamps can save you a lot of time and hassle!
Continue reading “Working with Dates & Timestamps in PHP”

Why does php not show errors? – How to display PHP errors when display_errors is disabled

OK, a simple thing I know. But one that always occurs.

When using shared hosting (or even your own dedicated servers), those managing those servers do their utmost to make them secure. Among many things this usually includes turning off ‘display_errors’ in the php.ini file.

However, when you’re trying to develop things it is usefull to display errors and warnings, but if ‘display_errors’ is off, you will be scratching your head wondering what’s going on and whats going wrong!
Continue reading “Why does php not show errors? – How to display PHP errors when display_errors is disabled”

Beginning your journey in Object-Oriented Programming using PHP

Object-Orientated programming is not a new idea! It’s been around and available for a long time in many languages.

With each new version of PHP, new features appear, while existing features are improved. PHP’s object support has been hugley improved since it’s introduction in PHP3 (yes it has had it all this time!). And so with PHP’s object support maturing, the reasons developers might not take an object orientated approach are getting smaller. More recently PHP5′s take on objects is much more powerfull and usable than it ever has been for PHP users – but still not without it’s faults.

A common problem that experienced (and new) PHP developers have when embarking on their object-orientated (OOP) journey is thet they don’t know WHEN to use it properly! As with all things there is a time and place for everything.

For those who do not know about OOP I am not even going to attempt to explain what it is! But fact you have found this article means you probably do, but incase not Wikipedia has good general background info on OOP.

Continue reading “Beginning your journey in Object-Oriented Programming using PHP”

Session Handling in PHP

Traditionally HTTP is a stateless protocol. That is it is made up of requests and responses and there is no notion of a ‘persistent connection’. This means that there is no way to have consistency or personalisation on the web since there is no way of knowing whom the request came from.

There are 2 primary methods – regardless of platform – that are used as a work-around for this.

First is a cookie. A cookie is a text file that lives on the client’s computer that store values set by your application. In general the file can only be access by the website/domain that issued it. The main problem with cookies is that they are un-trusted. A malicious user could modify the data and cause you problems!

The second – and better – solution is sessions. Similar to cookies, but they reside on the server, and cannot be directly modified by the client. When sessions are used, a cookie is still issued, but this simply holds the ‘session id’. This session id is generally sent by any request after it is created by the app including actions such as form submission. This ‘session id’ is a link to either a file or record on the server – depending how they are stored.

Continue reading “Session Handling in PHP”

Need to make PHP ‘stop’… PHP wait timer

AJAX is great… but sometimes it’s too quick if you’re updating… So… How can I make the page wait for a few seconds, then proceed?

Well there are 2 functions PHP has to pause execution. They are:

sleep(NUMBER_OF_SECONDS);

and:

usleep(NUMBER_OF_MS);

One thing you may want to keep in mind is that if you are using a for loop and wish to do this, then you might want to increase the timeout:

set_time_limit(NUMBER_OF_SECONDS);

PHP – Generate/Create strong passwords, uuid, random string

Very often the need to create passwords or some other ‘unique’ string be it a captcha or for use as unique identifier (UUID).

Here are a few functions I’ve used and refined over the years, 2 types, for password/random string generation and UUID generation.

Generating random strings is pretty simple as it’ll be a password.

However for a UUID there is always the slim chance it’s not unique.
Continue reading “PHP – Generate/Create strong passwords, uuid, random string”

Signs of poor PHP ‘products’

If you are a professional developer (or aspiring) you will inevitably get asked to customise or even better “extend” existing wares.

If it’s a product you are familiar with great! If not I heartilly reccomend spending a little time investigating prior to accepting a job ( ultimatley if they are happy to give

access to their code, if not atleast tell you what’s been used so you cam repliocate it to play with briefly). I know this sounds like a lot of work wich ultimatley – if you

don’t accept the job – you have done for free, but it will save you masses of grief, tears and late nights if you do.

So, I have a few pointers to ‘be aware of’ while evaluating the job and to (generally) identify crappy PHP products/solutions quickly…

Continue reading “Signs of poor PHP ‘products’”

PHP Form Action Self submit

A question I am always asked is about ‘self-submitting’ forms.
Continue reading “PHP Form Action Self submit”