Tag: PHP
-
How to check if a variable is an integer with PHP
PHP has a variety of functions and approaches to determine if a variable is a number and/or an integer. The problem is that many of the straight forward approaches can result in false positives depending on how they react to type juggling. Case in point, is_int() will return false for…
-
How to Strip Whitespace from a String with PHP
Whitespace, it can make all the difference in the world when attempting to determine if a string is empty or not. Anytime you are working with user input I recommend stripping whitespace from both sides of the string. When stripping whitespace you can choose to strip from both sides or…
-
How to Increment / Decrement a Number with PHP
Incrementing and decrementing a variable can be accomplished with C-style pre- and post-operators. The pre-operators return the value after it as been incremented or decremented, post- return the value before. Let’s take a look at incrementing first: $i = 1; echo ++$i; // outputs 2 echo $i++; // also outputs…
-
While you were out – while loops in PHP
while loops are considered the simplest type of loop in PHP-dom. Their purpose is to continue to execute until a condition has been met. Let’s look at an example related to the title, a pseudo-script that will collect messages while you’re out, returning at 1pm (like a lunch break ;):…
-
New Lines in Command-line PHP Output
If you’ve ever worked with command-line PHP and outputted text you know that PHP takes absolutely no liberties with injecting line breaks into the output. There’s also no magic function or configuration variable that will adjust this behavior. Easy enough, you want line breaks, just add them! echo “I am…
-
How to Output Strings in PHP
Outputting strings is one of the most common actions when building a web site, and PHP offers a multitude of ways to do so each with their own set of strengths and weaknesses print print is a language construct and not a function, so you don’t need any parenthesis to…
-
How to Capture User Input from Command-line PHP
When using command-line PHP capturing input is a bit different than when building a web application. Instead of building a form and interrogating some variables, you have to actually prompt the user for the input and then analyze it to determine if what the user input was what you were…
-
How to Generate a Random Number with PHP
Generating a random number in PHP is very simple and like most things, can be done a number of different ways. The old standby is to use the function rand() to to generate a random integer between the minimum and maximum values that you provide. To generate a number between…
-
Using Transactions with PHP Data Objects (PDO)
With PHP Data Objects, transactions are pretty simple. To be able to use a transaction, you simply wrap your queries with a call to beginTransaction() and either use commit() or rollBack() at the end. Rolling back is at your discretion but the easiest implementation would be to wrap the queries…
-
How to Use Prepared Statements in PHP
Now that we know how to create a connection to our database using PHP Data Objects, let’s talk a bit about prepared statements. Now I’d like to think that we’ve all made this mistake before, taken user input and used it in an SQL query something like this: $db->execute(‘SELECT *…