Category: Software Development
-
How to Terminate a PHP Script
Terminating the execution of a script is usually one of those last ditch ways to handle an error, but it is something we do from time to time, especially if we’re debugging. To terminate the execution of a script, you can call either exit or die, die being an alias…
-
How to Pluralize Words with PHP
There’s a good chance you’ve seen it before (and possibly authored it), it looks something like this “You have 1 new messages” or “There are 10 user(s) logged in”. What you end up with is something that’s either incorrect or something like looks a bit amateurish. To fix this, we…
-
How to Output Variables within Strings with PHP
Displaying strings isn’t that hard, you just use echo followed by the string in question. But that only gets us so far, what about when we need to use variables inside of the string? Luckily there’s a ton of different ways to accomplish this. echo You can easily use echo…
-
How to Use Colors in Command-line PHP Output
Nothing spruces up command-line output the way colors do. Just like when you’re customizing your [Bash] prompt, you can use color codes in strings to colorize your output. Keep in mind, these colors will only work on the command-line and not in a browser. First let’s take a look at…
-
Why Limit the Format of a Password?
I’ve been moving towards using longer and longer passwords. When I say longer, I mean upwards of over 20 characters, mixed case, numbers and special characters. These longer passwords make me feel a bit more secure, especially when I can also leverage two-factor authentication on top of it. Know what…
-
How to Compare Numeric Variables with PHP
Comparing numeric values is a very common programming task. Which number is greater? is lesser? equal? In PHP like any C-style language, you can do these comparisons with a simple if ( … ) { … } statement: if ($var1 > $var2) { echo $var1 . ‘ is greater than…
-
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 ;):…