Simple scripts in php for beginners

Printing Hello World
<?Php
echo "Hello World";
?>


Printing Pattern of * using for loop
<?Php
for($i=0;$i<20;$i++){
for($j=0;$j<$i;$j++){
echo '*';
}
echo '<br>';
}
?>

Demo
Printing welcome message and reversing the user entered string
<?Php
echo "<form name=f1 method=post action=''>
<input type=text name=t1><input type=submit value='Submit'></form>";
@$t1=$_POST['t1']; // Collect the string as entered by user
if(strlen($t1) > 1 ){ // If the length of the string is more than 1
echo "Welcome $t1<br>
Your name in reverse : ".strrev($t1);
}
?>

Demo
Length of a string in PHP
<?Php
$string= "Welcome to plus2net.com";
echo $string."<br>";
echo "Length of the string : ".strlen($string);
?>
Demo
Changing first character of the string to uppercase
<?Php
$string= "Welcome to plus2net.com";
echo $string."<br>";
echo "Length of the string : ".strlen($string);
?>
Demo
Multiplication table of a number
<?Php
$v1=5;// Change this number
for($i=1;$i<11;$i++){
echo "$v1 x $i = ".$v1*$i;
echo '<br>';
}
?>
Demo
Sum of two numbers
<?Php
$v1=10;
$v2=30;
echo "Sum of two numbers $v1 + $v2 = ". ($v1 + $v2);
?>
Demo
Displaying Even Numbers
<?Php
for($i=1;$i<=20;$i++){
if($i % 2 ==0){
echo $i;
echo '<br>'; // Line break
}
}
?>
Demo
Highest of four Numbers
<?Php
$v1=68; $v2=12;$v3=135;$v4=22;
echo "Four Numbers are : $v1,$v2,$v3,$v4 <br>";
echo " Highest Number is : ".max($v1,$v2,$v3,$v4);
?>
Demo
Print todays Date and time
<?Php
echo date("m/d/y : H:i:s", time());
?>
Demo
Displaying last 20 days calendar details
<?Php
$m= date("m");
$de= date("d");
$y= date("Y");
for($i=0; $i<=20; $i++){
echo date('d-M-Y:D',mktime(0,0,0,$m,($de-$i),$y));
echo "<br>";
}
?>
Demo

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Google+

Gaurav

16-07-2014

Hi

How yo print below using for loop logic

1
2 5
3 6 8
4 7 9 10
subhpurohit

28-06-2017

please give details in programs

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer