Category: Software Development
-
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…
-
Version Control for Individuals
I find with a lot of folks I interact with, version control is considered something that you use when you are working on a team with multiple contributors. As an individual that is usually the sole contributor of my own projects, I have to disagree as version control is still…
-
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…
-
The Importance of a Logout Page
Running social networks have proven eye opening in regard to how users interact with a website. One thing that I’ve noticed that I didn’t necessarily expect is that quite a few people click the logout button. Perhaps it’s because they have more than one account (tsk tsk as we don’t…
-
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 *…
-
How to Connect to a Database with PHP Data Objects (PDO)
PHP Data Objects (PDO) provide a consistent interface to different databases including MySQL, PostgreSQL, SQLite, SQL Server and a handful of other systems as well. Aside from database-specific SQL, the only other thing that isn’t consistent is how to connect to a database. Each database type requires a string to…
-
Split a String by Letters with PHP
Every once in a blue moon you find yourself needing to take a string and converting it to an array of characters in the string. Maybe it’s because you want to count how often certain letters exist in the string or want to convert the letters to something else. To…