360ideas
316-269-6920
Get a Quote

The five PHP tricks every web designer should know

Tuesday, January 20 2015 1:31 PM
By Kalen Kubik
The five PHP tricks every web designer should know

Over fifty percent of web designers are privileged (or cursed) enough to code out their own designs. Many of us understand the nuances of HTML and CSS enough to be pixel-perfect front-end coders. However, when it comes to Wordpress and other CMS systems, web designers must work with and around PHP, a server-side language. PHP is full of tricks you can incorporate into your CMS or static websites that will make your life much easier. To initiate PHP, all you need do is change your file extensions from .HTML to .PHP

* I apologize in advance, my blog system does not have the <CODE> or <PRE> tags available.

 

1. Included headers and footers

A basic PHP function most websites are built around is the include function. The include function allows for the removal of the header and footer code from every page file. The header and footer files are then stored in a separate folder, where they can be referenced through the include function call. Edits made to the single header or footer file take effect site wide, as all pages reference these included files. Includes save you a monstrous amount of time, saving you from editing twenty files just to add an extra link.

 php file tree diagram

Your file tree will need to be setup correclty for this to work and your files must be .php.

 

2. IP If statement testing

If statements are common conditional statements in PHP. In this specific statement, we're instructing the server to show the code contained within ONLY to the specific IP in the If statement. You can use this for testing on live websites or for April fool pranks. For testing, visit Google and search "What is my IP Address" Google will respond with your IP and you may use it in the code so that only boxes visiting from your IP address can see the code. Replace the 000.000.000.00 with your own IP.

<?php if($_SERVER['REMOTE_ADDR'] == '000.000.000.00') { ?>
    Your HTML testing code here.
<?php } ?>

Make sure you're using two equal signs. To test, change the If statement's IP address from yours by one number and refresh the page to ensure that all other IP's cannot see the test code.

 

3. Setting Meta Variables in Included Headers

PHP makes managing a header and footer manageable. But it doesn't matter how convenient the header is if you can't store page specific meta titles and meta descriptions. In order to pass the meta titles and meta descriptions to the header we need to make them PHP variables, then reference those variable in the header. This will make the meta information unique to each page, even with the shared header.

First, we create the variables on our individual pages. We're going to add our variables to our open php tag that also contains our header include.

PHP meta variables

Each page now has its own unique meta information. You can use this to create page specific open graph tags as well for social media and the like.

 

4. URL If - Else If - Else Statement for Template Header Images

Many times a CMS template will not allow you to set a custom header image for each page that uses said template. Even in Wordpress, sometimes you are forced to use a custom module. However, there is an easy way to assign header images to pages based on their URL's, allowing every page-specific URL of your website to operate off of the same template yet maintain its own header image. This can be used for any URL specific include. As this is server-side code, you're only loading one of the images per page. Admittedly, this is a hack for when you have little control over the templating system.

 php header images

 

 5. $home = true;

Many times a website's homepage is weighed down with scripts and heavy functionality. These scripts are often left in the footer, where they load on other pages unnecessarily. To eradicate this issue, tag your index.php with a variable. I use "home". set the variable to true above your header include tag. Then, in your footer, creat an if statement for home. This will ensure the index specific scripts will only load when the variable is true. This will save your page load times and keep you from loading bulky, unused scripts on every other page.

PHP home true variable

 

Understanding PHP is extremely useful as you make your way from web designer to web developer. Even if you use another language besides PHP, you'll note that they are all quite similar. Good luck out there and happy coding.

Love, 360

Share the love

Submit Your Comment