Tag Archives: PHP

require() or include() with PHP?

A lot of people mis-use these two functions. In short, the difference is this

  • require() “requires” a file to be “included” before it proceeds, if the “required” file is not found, script execution halts returning a fatal error.
  • include() “includes” a file if it can find it, if not, it returns a warning explaning why (usually just bad file name)

Which one is more important is really up to you, but I would by default recommend using include() over require() just less mess to deal with.

Extending the include functions
PHP added this cute functionality to the include functions called “_once”, so you could include_once() or require_once() a file. They both do the same thing they’ve always done with an exception, they only include a file once. Pretty hard to understand isn’t it? that require_once only requires a file once. Pretty original :) But seriously, this function used to save my life until I realized that it was simply bad design (most of the time) that caused the problems that “_once” functions were created to fix. In other words, If I’m including a file more than once anyways, I need to fix that problem instead of just being lazy and using PHP’s little include_once() speciality function. Now I’m not arguing that they made it, and it ships with PHP so they must still have it in circulation for a reason, yes, you’re correct.

Someone in ##php pointed out to me that although it might come in handy, every time you use include_once or require_once PHP has to look through the list of files already included and check their contents to make sure it hasn’t already included them. (read that again slowly.) Which narrows itself down to a lot of performance wasted because you were too busy to re-design your application a little bit to only include a file once. Think about it, you’re putting the statement there anyways, how hard is it to just make sure you aren’t duplicating it? I posted an article about duping your PHP code here, you should probably read it. It will help you with other things.

In closing, I use include() functions by default and require() if I REALLY need a file. Even then, I just use include(). So, now you know.

“Quotations” & Concatenation in PHP

So maybe you’ve seen people use single quotes, maybe double quotes? Personal preference? Wrong. Processing time? True. Also, you’ve seen them use this crazy “.” thing in between variables and strings, or maybe even entire functions?! Yes, that’s called concatenation. (con-cat-in-ation) yes, it’s a mouthful but it’s extremely useful.

“Quotations”
In PHP there are two types of quotations (actually 3 but I’ve never met anyone that uses the third, so, I’ll just tell you it’s the “ and unless you have some stupid infatuation with them, just use normal single or double quotes.)
The two types we are going to talk about in this article are

  1. Single quotations
  2. Double quotations

NOTE Sometimes they are referred to as, “unparsed quotations” and “parsed quotations” you’ll know why soon.
Single quotations (unparsed quotations) – ‘ ‘
In PHP single quotations can encapsule (hold) any type of data, numbers, decimal, strings, variables, it doesn’t matter. The difference here is when we feed it a variable to hold alongside a string or something, for example:
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is $name’;
?>
Instead of actually saying “what’s up, my name is crainbandy” this will say, “what’s up, my name is $name” because single quotes turn a built-in PHP function called “variable parsing” off. Meaning, instead of looking for $variables to replace with their corresponding values, they just use the raw text which is $name in the case of the script above. Make sense? In other words… “Single quotes don’t replace variable names with their corresponding value.”

Double quotations (parsed quotations) – ” ”
This is the opposite of single quotations in the fact that, they replace variables with their corresponding value.
Example
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is $name’;
?>
This will actually say, “what’s up, my name is crainbandy” instead of “what’s up, my name is $name“, the opposite result of single quotes.

The main thing I want you to get out of this is the fact that PHP has to turn on a lot of referencing and underlying matching if you use double quotations, whether or not there is a variable involved. You think PHP just somehow knows that a variable has a VARIABLE for a value? No. PHP has to look.

For example,
<?php
$name = “crainbandy”;
echo $name;
?>
Why in the world would you want to parse a solid string with double quotes when you could have just as easily used $name = ‘crainbandy’; instead and gotten the same result? How much processing time and performance you lose by things like this I’m not sure, but you do lose some obviously.

So now you know the difference. If I look at your code and I see variable assignments to solid values with double quotes I’m going to e-slap you.

In a nutshell, use single quotes! Unless for some reason you need to “parse” the strings, fine, but I still think you can use the next big word “concatenation” to get around that.

Concatenation (Or a period .)
This is such a cool feature, I don’t know where I would be in my scripts without it. As you can see in my Constantly use Constants in PHP article, when I’m defining constants I use a combination of single quotes with concatenation. It’s cool, check it out.

For example, usually to echo a variable value to anything you would have to use double quotes.
Example:
<?php
$name = ‘crainbandy’;
echo “what’s up my name is $name”;
?>
This would produce the desired effect right? Of course it would, but, there’s something more we can do with this simple script.

Concatenation! Concatenation simply means “Attaching a value to a variable” by means of… “concatenating” it.
Example
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is ‘ . $name;
?>
You like that? You FREAKING like that? That’s what I thought. Yes, you’re observant. “What in the world is that backslash doing in there?” Well my friend. That is called “escaping strings” and this article is not about that. Sorry. Back to concatenation.

So there you have it, PHP actually knows that when you have a concatenated string like the one above, just continue going on replacing values/running methods and or functions until you reach that beautiful semi-colon. Why would you use this instead of the “time saver” that double quotes is? Well for several reasons

  1. You cannot use arrays values inside single quotes or double quotes (you can with double quotes, but you run into a problem)
  2. You can’t run functions or methods inside a double quoted or single quoted string. With concatenation, you can do that.

Running a function inside an echo statement with PHP
<?php
$name = ‘crainbandy’;
echo ‘what’s up, my name is ‘ . strtolower($name);
?>
You see how that works? Cool. Glad. Psyched. Riled. Sweet

Usually in your PHP scripts you will be using a mixture of single quoted, double quoted, and concatenation. Sometimes there’s just no reason to use concatenation and make yourself go through the extra typing, but, it usually is more resourceful.

One other way to concatenate strings is with the .= operator.

$name = ‘wax’;
$name .= ‘jelly’;
This creates a variable called $name that has the value “crainbandy”, pretty cool huh? Okay. Til next time…

Constantly use constants in PHP

I’m going to talk to you about a few ways to use PHP’s constants to save yourself a lot of time and hassle.

PHP has this cool thing called a “CONSTANT” that basically lets you define a string to represent a value that is available everywhere. it basically doesn’t have a variable scope.

Note: a constant cannot be an array. It can be anything else, just not an array.

Syntax
define(’CONSTANT_NAME’, ‘VALUE’);

In a nutshell, a constant can be useful for anything from the maximum amount of failed logins you allow before prompting for CAPTCHA or simply banning their IP temporarily.

For example, Instead of having to type crainbandy.com/link or if you’re linking a stylesheet like http://www.crainbandy.com/style.css, or anything you can just use a constant to represent it.

The good thing about using constants inside your scripts is that instead of changing your entire script to adjust URL changes or any type of change, you can just change the constant.

Compare the two pieces of code below:

<?php
include(’/crainbandy/header.template’);
include(’/crainbandy/navigation.template’);
include(’/crainbandy/body.template’);
include(’/crainbandy/secondary.template’);
include(’/crainbandy/footer.template’);
?>


Read more

Don’t dupe your PHP code

A common problem I see among new(er) programmers, especially to a both loosely and dynamically typed language like PHP is something I’m not exactly going to coin, but more so call “duping” or a “dupe”

Basically this is when people just assume that by aliasing or creating new variables over and over and performing multiple functions across 28 different variables then bringing them together. They could have just used a single variable and a one line solution (The Perl guys love these things, they’re called one-liners. Notorious for doing huge amounts of data manipulation and returning results in only one line of code; and no, wise guy, the “one-liner” doesn’t turn the scrollbar into a mustard seed.)

So basically it would boil down to this, lets say you wanted to get a hash using sha1() of a string, then you wanted to sha1() it again, then once more and then return a little “key” with a set string length (For whatever reason, cool, but it’s just an example for right now.)
The code might look something like this:

// return a string’s hash
function three_hash($str) {
// hash the string
$hash = sha1($str);
$hash = sha1($hash);
$hash = sha1($hash);// split the key
$key = substr($hash, 0, 10);

// return the hashed split key
return ($key);
}

Now the majority of you say “Wow, that’s dumb…who would do that?” Well surprisingly a huge amount of progammers (including some of my old code I look through, you know it’s bad when you wrote it and you look at it and say to your self “What in the world is this guy doing?” then you realize it’s yours. So to help some new-comers to the programming world, I’m writing this.
The code above could have just as easily been this:

// return a strings hash footprint
function three_hash($str) {
return (substr(sha1(sha1(sha1($str)))), 0, 10));
}

Granted, it might not be as “pretty” but to me it is. Just something to keep in mind when you’re making functions, or just writing little scripts. Even though this was an extreme example, it still applies to just about anything. The more corners you cut and the more variables you eliminate the less processing PHP has to do and the more you can be on your way to speeding up your codes run-time considerably.
Just a thought.
-bryan