Program to find prime numbers using PHP

This is a simple program in PHP to find prime numbers using for loop and if  statement.

<?php
error_reporting(E_ALL);
//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo “Prime Number : “, $j, “<br>”;
}

OUTPUT

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

 

Posted in PHP

Fibonacci Series in php

Fibonacci series is a sequence of number generated by adding the previous two terms.

Example-

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;
}

Output –

1 2 3 5 8 13 21 34 55 89

Posted in PHP

When use of back ticks in table name solves my problem in PHP/mysql

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.

table with a dot

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','');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('stcktest', $conn);

$result = mysql_query("SELECT * FROM `abc.test`");

//print_r(mysql_fetch_assoc($result));

while ($row = mysql_fetch_assoc($result) )
{
echo "Id is : ".$row['id']." "." Name is : ".$row['name'];
echo "
";
}

Difference between echo and print in php

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("Hello World")
print("Hello World")

echo can output more than 1 string while print cannot(using comma separator)

echo "Hello", "world","I am Swapnesh";
print "Hello";
print "World";

How to send email to multiple recipients in magento


$postData['offemail'] = 'sendermail@gmail.com';
$templateId = 1;
$recipients = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT);
$recipients = explode(',' , $recipients);
array_push($recipients,$postData['offemail']);
$store = Mage::app()->getStore();
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')
->sendTransactional($templateId,
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
array_values($recipients),
'Recipient Name',
array('data' => $postObject),
$store->getId()
);
$translate->setTranslateInline(true);

System.out.print in php

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(“<br>”);
system.out.print(“This is a valid system.out.print with lowercase s”);
System.out.print(“<br>”);
System.out.print($result);