How to post Source Code with syntax highlighting in WordPress?

I have been posting some web-dev help, PHP scripts and CSS styles codes on my Sixth Sense India blog. Earlier, I use to put PHP codes under <blockquote> </blockquote> tags, but found that it does not look imperessive and is not easy to read or copy from the post page. Recently, I came to know about a way to post Php Source code on to blog post. You can use a plugin SyntaxHighlighter Evolved and publish your Code snippets like this

[sourcecode language=”php/css/javascript“]
<?
//Put your Code Here
echo “Hello World”;
?>
[/sourcecode]

It will render like as its shown here below

<? 
//Put your Code Here 
echo "Hello World"; 
?>

Generating random HTML Code / Ad banner with PHP

Many times,  I feel the need to use a code to display random content on my websites. There are many options available to achieve this.  Web developers use different scripts and applets for generating random HTML code on to their websites.  Though Javascript is very popular for this purpose and sample codes can be downloaded from web resource sites like DynamicDrive or Javascriptkit.  Now all most every web browser support javascript, so it becomes first choice for web developers. With the gaining popularity of hand held/ mobile devices world over, more and more people now use their smartphones to surf web and send email etc, but not every mobile browser support javascript and thus your content will not be available for users to view. To rescue from this situation web-developers started implementing Server Side Scripting to display special category of content like serving ads on their web pages. I  prefer PHP script which is also a Server Side Script. PHP is relatively easier language to implement into existing web pages. Being an Open Source Language, PHP is supported by mostly every webserver/web-hosting providers. Here is a PHP scipt code that help you generate random HTML code on to your web page.

<?php
//PHP Random HTML Script

$random_number = rand(1, 2); // number of banners to shuffle

$random_banner[1] = &#039;<a href="http://shaadiwala.com" target="_new"><img class="wp-image-1232" src="http://shaadiwala.com/images/creatives/shaadiwala_banner_200x200.jpg" alt="" border="0"></a>';
$random_banner[2] = '<a href="http://shaadiwala.com" target="_new"><img src="http://shaadiwala.com/images/creatives/shaadiwala_box_200x200.jpg" alt="" border="0"></a>;';

// print random banner on web page
echo ("$random_banner[$random_number]");
?>

This very useful PHP script for every web-developer to keep in his Script pile.

Learn PHP : Making a connection to MySQL database

The benefit of choosing PHP as a web platform is its utility in dynamic and database driven web applications. It is fairly easy in PHP script to make a connection to MySQL database.

<?php
$connection =  mysql_connect("DataBase-Host", "DataBase-user","Password-of-databse") OR die('Error connecting to mysql'); //means do not execute code any further.
$dbname = 'databasename';
mysql_select_db($dbname);
?>

There is one more better way to go with above code statement.
Good practice is to make a single file which can be called as and when it is required.
like you can save a file with a name e.g. connection.php with following code in it and can include it in any file to have a Database connection.
Here we will assign values to variables and it can be handy when you start coding more and more. You do not have to type data base connection script code every time just use include function and code will become part of that script.
e.g. <?php include “config.php”;?>
You can edit parameters to your own database values.
Save this file as config.php

<?php
// This is an example of connection.php
$dbhost = 'localhost';
// HOST OF DATABSE generally its localhost
$dbuser = 'root';
// USER OF DATABASE
$dbpass = 'password';
// PASSWORD of the Database User account
$dbname = 'learnphp';
// Name of Databse
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die  ('Error connecting to mysql');
mysql_select_db($dbname);
?>

Learn PHP : Lesson 1 – Get it Easy

Ok you wanna learn PHP. Who knows you might be running next big thing. What ever! lets start first lesson. Your first code in PHP scripting language. I would advise you to learn a bit of HTML before you jump into PHP coding thing.

Like HTML, PHP also lives in tags known as open and close tags. <? is the open or start tags and ?> is close tags. Every PHP code stats with open tags and ends with close tags. In PHP 5 version coders are more inclined to use <?php open tags rather than <?

There are some really simple script commands in PHP like PRINT, ECHO, SET, GET, POST, FOR, WHILE etc. Just like ordering someone to do a certain job for you. Yeah! just like that. 🙂

Before you begin coding PHP you need to download a few Freeware softwares from Continue reading “Learn PHP : Lesson 1 – Get it Easy”