Beginner exercises are most useful when each example demonstrates one idea at a time. The example below uses current input handling without the error-suppression operator and escapes user supplied text before sending it back to the browser.
<?php
$name = trim($_POST['name'] ?? '');
if ($name !== '') {
echo 'Welcome ' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
}
?>
The practice programs below cover loops, strings, arithmetic, dates and form input. Follow the individual demo links to see each idea separately.
<?php
echo "Hello World";
?>
<?Php
for($i=0;$i<20;$i++){
for($j=0;$j<$i;$j++){
echo '*';
}
echo '<br>';
}
?>
Demo<?Php
echo "<form name=f1 method=post action=''>
<input type=text name=t1><input type=submit value='Submit'></form>";
$t1=trim($_POST['t1'] ?? ''); // Collect the submitted string
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<?Php
$string= "Welcome to plus2net.com";
echo $string."<br>";
echo "Length of the string : ".strlen($string);
?>
Demo<?Php
$string= "welcome to plus2net.com";
echo $string."<br>";
echo "After changing : ".ucfirst($string);
?>
Demo<?Php
$v1=5;// Change this number
for($i=1;$i<11;$i++){
echo "$v1 x $i = ".$v1*$i;
echo '<br>';
}
?>
Demo
<?Php
$v1=10;
$v2=30;
echo "Sum of two numbers $v1 + $v2 = ". ($v1 + $v2);
?>
Demo<?Php
for($i=1;$i<=20;$i++){
if($i % 2 ==0){
echo $i;
echo '<br>'; // Line break
}
}
?>
Demo<?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<?Php
echo date("m/d/y : H:i:s", time());
?>
Demo<?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>";
}
?>
DemoAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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 | |