This is a simple program in PHP to find prime numbers using for loop and if statement.
//Program to find prime numbers in between a range
for( $j = 2; $j <= $num; $j++ )
for( $k = 2; $k < $j; $k++ )
if( $j % $k == 0 )
if( $k == $j )
echo “Prime Number : “, $j, “<br>”;
Prime Number : 2
Prime Number : 3
Prime Number : 5
Prime Number : 7
Prime Number : 11
Prime Number : 13
Prime Number : 17
Prime Number : 19
Prime Number : 23
Fibonacci series is a sequence of number generated by adding the previous two terms.
1, 2, 3, 5, 8, 13, 21,….. and so on.
At a first glance you can check that, starting from 3rd term its an addition of previous two values.
You can also write a simple program in PHP for this series generation –
$a = 1;
$b = 2;
$term = 10;
$i = 0;
echo $a." ".$b." ";
for($i; $i < $term -2 ; $i++)
$c = $b +$a;
echo $c." ";
$a = $b;
$b = $c;
1 2 3 5 8 13 21 34 55 89
While playing with the code today, I found an interesting benefit of using back ticks in the table name in mysql query.
I created a table in my database as “abc.test” and added some dummy data to the table as shown in below screenshot.
Now with a simple mysql_query I was trying to fetch the data but its returning nothing, after a check I found that i missed those back ticks from my table name.
Incorrect Query (without back ticks)
mysql_query(“SELECT * FROM abc.test”);
Correct Query (with back ticks)
mysql_query(“SELECT * FROM `abc.test`”);
My whole working php code, to fetch data –
$conn = mysql_connect('localhost','root','');
die('Could not connect: ' . mysql_error());
$result = mysql_query("SELECT * FROM `abc.test`");
while ($row = mysql_fetch_assoc($result) )
echo "Id is : ".$row['id']." "." Name is : ".$row['name'];
echo and print are both language construct in php, however print returns 1 but it is not a function.
Below mentioned usage of echo and print, both are valid –
echo "Hello World"
print "Hello World"
echo can output more than 1 string while print cannot(using comma separator)
echo "Hello", "world","I am Swapnesh";
$postData['offemail'] = 'firstname.lastname@example.org';
$templateId = 1;
$recipients = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT);
$recipients = explode(',' , $recipients);
$store = Mage::app()->getStore();
$translate = Mage::getSingleton('core/translate');
array('data' => $postObject),
This is the code that i used during one of my projects to change date from Melbourne timezone to Perth datetime zone.
$mydate = $backsymbols[$i][‘symboldate’];
$date = new DateTime($mydate, new DateTimeZone(‘Australia/Melbourne’));
echo $date->format(‘M d, Y h:i:s A’) . “\n”;
MySQL Server doesn’t support the SELECT to make table backup, so this query is not going to work in phpmyadmin –
However to take table backup, run this query –
CREATE TABLE default_table_backup_31_08_2012 SELECT * FROM default_table;
This is my table structure, and my aim is to fetch data from the single table in parent -> child -> parent manner —
MY Query —-
And Finally, my RESULT —
System.out.print is totally valid in php likewise in JAVA. I ran this sample code and its working all fine in PHP 5.3.
$result = 5;
System.out.print(“This is a valid System.out.print with uppercase S”);
system.out.print(“This is a valid system.out.print with lowercase s”);
Little working code to understand array_sum() and end() functionality —
$a = array(1,5,8,0,4,3);
$b = array_sum($a);
if($b < end($a))
$c = end($a)+ ($b/2);