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);
?>

Script Code : PHP Sessions Starting, Setting and Closing

PHP Tutorial [Beginner’s Basic learning] – Sessions in PHP. Starting, Setting and Closing

Session(s) is a temporary storage of values/data to variables to be used for serving web user in a better and interactive way. The data stored in sessions can be either user provided or is being drawn from a stored database at server end.

Starting Php Session

Starting sessions in php is very easy and it is initiated by simple script code as shown here below

<?php
session_start(); // start up your PHP session!
?>

Displaying Values from PHP Session


<?php
session_start(); // start up your PHP session!
$_SESSION['user'] = "Guest"; // store session data
echo "Your Name is = ". $_SESSION['user']; //retrieve data
?>

above code will output the following line
Your name is = Guest.

Setting Session with isset

<?php
session_start();
if(isset($_SESSION['hits']))
$_SESSION['hits'] = $_SESSION['hits']+ 1;
else
$_SESSION['hits'] = 1;
// Now output page hits
echo "hits = ". $_SESSION['hits'];
?>

Closing Session in PHP coding

We have two options. Either we can remove session values with unset function like shown in PHP code given here below:

<?php
session_start();
if(isset($_SESSION['userID']))
unset($_SESSION['userID']);
?>

OR
We can destroy session with session_destroy function as shown here under :

<?php
session_start();
session_destroy();
?>