Simple scripts in php for beginners

Start with small, testable PHP programs

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>";
}
?>
Demo

More Basic scripts in PHP

Check the string or number is Palindrome or not in PHP Factorial of a number Printing number from 1 to 50 by for loop Sum of digits of a number Strong Number Armstrong number
Fibonacci Series
Introduction to PHP


Subscribe to our YouTube Channel here



plus2net.com







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




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer