Site icon Taiijas Infotech Private Limited

Awesome Tips and Tricks for PHP Beginners

Ok, so now you have “learned” …. wait should that be a single quote …. PHP and you are ready to really be able to start using it for something more than you can do with HTML. I will get into the use of single quotes vs double quotes, short tags, inline if statements, a few other little tips and tricks.

Single Quotes vs Double Quotes

You may ore may not have even thought about this up to this point in your PHP use but it is kind of a big deal. Using the right or wrong quotes can not only cause errors that might be hard to find but there can be a slight performance boost and can make your code much easier to read.

<?php
     echo 'hello world';
?>


<?php
     echo "hello world";
?>


These will produce the exact same end result but do you know which one is technically better? The single quote. The single quote just puts exactly what is inside of it without processing while the double quote actually evaluates and processes. Let me explain with an example.

<?php

      $example = 'hello world';

      echo '$example'; // outcome will be $example

      echo "$example"; // outcome will be hello world
?>


As you can see here the double quotes take the time to process what is inside so technically there will be a little more overhead. Obviously on a small scale this means nothing but if you have a loop that iterates 1000 times you might start to see the benefits of single quotes in a performance sense.

Now if you are trying to put a variable in a sentence you can do this:

<?php

	$example = 'hello world';

	echo 'This is my '.$example.' for PHP';  // outcome This is my hello world for PHP
	echo "This is my $example for PHP";  // outcome This is my hello world for PHP
?>


Either of these will work just the same in getting the output that you want but the single quote will be slightly faster because while both have to process the variable the double quote is also scanning the rest of the sentence in search for anything that it has to process. Yes it is more typing but I personally find the first one much more readable because you can much more clearly see that you are outputing a variable. If this was randomly in a 500 line PHP file the single quote method would be easier to spot.

PHP Short Tags

You know when you just want to prepopulate a form or add one PHP variable into the middle of a an HTML block. Well having to write an open PHP tag and then a close PHP with all of the line breaks and indentions to make it readable is such a pain. So what is the solution? PHP short tags like this.

<?php
	$example = 'This is some text that I want in my paragraph.';
?>
<html>
	<head>
	</head>
	<body>
		<h1>I love short tags</h1>
		<p>
		< ?php
			echo $example;
		?>
		</p>
	</body>
</html>
</pre>
Personally I like to get things done with as little typing and code as possible, so I am not a fan of this.  I love my short tags.
<pre>
<?php
	$example = 'This is some text that I want in my paragraph.';
?>
<html>
	<head>
	</head>
	<body>
		<h1>I love short tags</h1>
		<p>
		< ?=$example?>
		</p>
	</body>
</html>


That will produce the exact same output as the first but much neater and with a little less code.
NOTE: Short tags is something that can be disabled in the php.ini so it is not guaranteed to work but industry standard has short tags enabled.

Short Tags With Inline IF Statement

Short tags are also great when used with inline if statements. Having if statements in an area of the code with a lot of HTML can make it harder to read but using inline if with short tags can make it much neater.

<?php
	$minimum_age = 21;
	$customer = 19;
?>
	<p>You are
	<?php
		if($customer<$minimum_age)
		{
			echo 'NOT';
		}
		else
		{
			echo '';
		}
	?>
	old enough to buy alcohol here.</p>


Obviously we do not need the else in this case because it doesn’t do anything. I just have it in there to help understand inline IF statements. Also since there is only one output line we dont need the curly braces.

<?php
	$minimum_age = 21;
	$customer = 19;
?>
	<p>You are < ?=($customer<$minimum_age?'NOT':'')?> old enough to buy alcohol here.</p>


You see the inline IF is shorter but might be confusing at first so I will break it down.

<ul>
	<li>First "$customer< $minimum_age" is exactly the same as "if($customer<$minimum_age)"</li>
	</li><li>Next "?'NOT':" is the same as "{ echo 'NOT'; }" (anthing after the question mark and before the colon is executed if the statement is true.</li>
	<li>The last piece is the "else" ":''" is the same as "else{ echo ''; }" (anything after the colon is what is executed for the else)</li>
</ul>


And lastly for this post I just want to show a little way to dynamically create variables that can make things a little bit quicker for you. One of the best uses for it is when you are making a database call. Here is a traditional example.

<?php
     $id = '1';
     $query = mysql_query("SELECT name,title,content FROM blog_post WHERE id='$id' LIMIT 1");
     $data = mysql_fetch_assoc($query);
?>
     <div id="blog_wrapper">
          <h2>< ?=stripslashes($data['title'])?></h2>
          <span id="blog_author">< ?=stripslashes($data['name'])?></span>
          <p>< ?=stripslashes($data['content'])?></p>
     </div>

Here we run a query and are outputting the data in HTML format. We do stripslashes() on all of the items because when you insert things into the database it will ad slashes to escape special characters. Instead of having to individually do the stripslashes on everything we can do $$key = stripslashes() like this:

<?php
     $id = '1';
     $query = mysql_query("SELECT name,title,content FROM blog_post WHERE id='$id' LIMIT 1");
     $data = mysql_fetch_assoc($query);
	 if(is_array($data))
	 {
		foreach($data as $key => $value)
			$$key = stripslashes($value);
	 }
     ?>
     <div id="blog_wrapper">
          <h2>< ?=$title?></h2>
          <span id="blog_author">< ?=$name)?></span>
          <p>< ?=$content)?></p>
     </div>


Now you can see that we can access all of our items from the database like $name,$title,$content. We can do this because the $ in front of $key sets it as a variable with a value of stripslashes($value). Here we are taking the $data array from the query and looping through it. In the foreach($data as $key => $value), $key is the key for that element in the array (which is the reference like $data[‘name’]).

Share this:
Exit mobile version