Tutorialspoint

Php Programming Examples

Global vs Local Variables in PHP

php

<?php
  $x = 5;
  $y = 10;
  function test()
  {
     // $x = 10;$y = 30;
     global $x,$y;
      echo $x." ".$y."\n";
  }
  function test1()
  {
      $GLOBALS['x']+=$GLOBALS['y'];
  }
  test();
  test1();
  test();
  
  
  $nums = array(12,2,10,32,23,5);
  sort($nums);
  foreach($nums as $i)
  {
      print($i." = ".var_dump($i)."\n");
  }
  
  $num1 = array('abc'=>10,'abde'=>1,'efg'=>11,'bcd'=>20);
  
  ksort($num1);
  
  print_r($num1);
  
  foreach($num1 as $i)
  {
      echo $i."\n";
  }
  
  
  
?>

Execute PHP Online

php

<html>
    <head>
 
        <title>Create Table Dynamically</title>
        <style>
            
        </style>
    </head>
 
    <body>
        <form method="post" action ="main.php">
            <table border="1" width="350px">
                <tr>
                    <td>Enter number of Rows</td>
                    <td><input type="text" name="rows"/></td>
                </tr>
                <tr>
                    <td>Enter number of Columns</td>
                    <td><input type="text" name="columns"/></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                    <input type="submit" value="Create Table" name="create"/>
                    </td>
                </tr>
            </table>
        </form>    
        
        
            <span style="color:red"></span>
<?php
if(isset($_REQUEST['create']))
    { ?>
  <span class="comments">
    <?php $row=$_REQUEST['rows']; ?>
 <span class="comments"></span>
<?php $col=$_REQUEST['columns']; ?>
     <span class="comments"> </span>
        <?php echo "<table border='1' width='350px'>";
        for($i=1;$i<=$row;$i++)
        { ?>
 <span class="comments"></span>
        <?php    echo "<tr>";
            for($j=1;$j<=$col;$j++)
            { ?>
              <span class="comments"></span>
            <?php echo "<td>row".$i."col".$j."</td>";
            } ?>
                       <span class="comments"></span>
            <?php echo "</tr>";
            
        } ?>
       <span class="comments"></span>
        <?php echo "</table>";    
    } ?>
<span style="color:red"></span>
        
        
    </body>
</html>

equality

php

<?php

echo 0.00 == 0  ? 'true' : 'false';
echo PHP_EOL;
echo 0.00 === 0 ? 'true' : 'false';

Execute PHP Online

php

<?php

$data    = 'this is appLe and ApPle';
$search  = 'apple';
$replace = 'pear';

$data = preg_replace_callback('/\b'.$search.'\b/i', function($matches) use ($replace)
{
   $i=0;
   return join('', array_map(function($char) use ($matches, &$i)
   {
      return ctype_lower($matches[0][$i++])?strtolower($char):strtoupper($char);
   }, str_split($replace)));
}, $data);


?>

Execute PHP Online

php

<!doctype html>
<?php

$connect = mysqli_connect("localhost","root","");
mysqli_select_db($connect,"miniproj");
    
if(isset($_POST['submit'])){
	$user = $_POST['username'];
	$pass = $_POST['password'];
    
    for($i = 0; $i<strlen($user); $i++){
        if($user[$i] == "'"){
            echo("Don't try an injection attack. the cops are on their way!!!");
            exit();
        }
    }
    
    for($i = 0; $i<strlen($pass); $i++){
        if($pass[$i] == "'" ){
            echo("Don't try an injection attack. the cops are on their way!!!");
            exit();
        }
    }
    
    $query = "select * from users where id ='$user' and pass = '$pass'";
    $query_run = mysqli_query($connect,$query);
    
        
	if(mysqli_num_rows($query_run)>0){
		
        header('location:0_success.html');
	}
    else{
	echo("error ! please enter correct data!");
	}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post">
	<table align="center">
		<tr>
			<td>username:</td>
			<td><input type="text" name="username" placeholder="enter your username"></td>
		</tr>
		<tr>
			<td>Password:</td>
			<td><input type="password" name ="password" placeholder="enter your password"></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submit" value="submit"</td>
		</tr>
	</table>
</form>
</body>
</html>

Azza.Php

php

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
for($number =1; $number<100;$number ++){

if ($number%5==0) {
   echo $number.'<br>';}}
?>
</body>
</html>

Execute PHP Online Mohammed Elhadi SE4

php

<?php
   
  function familyName($fname, $year) {
    echo "$fname Refsnes. Born in $year ";
}

familyName("Mohammed", "1996\n");
familyName("Ahmed", "1997\n");
familyName("Essam", "1998\n");

$t = date("H");

if ($t < "10") {
    echo "Have a good morning!";
} 
elseif ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}

?>

suggestions

php

<?php 

$hour = '11';
$minute = '1';
$type = 'PM';

$startTime = date("H:i:00", strtotime(sprintf("%02d:%02d " . $type, $hour, $minute)));

echo $startTime; 
echo "\n\n";
echo date('h-i-s', strtotime($startTime));
echo "\n\n";
echo date('h-i-s A', strtotime($startTime)); // If am/pm is required


echo "\n\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    array_push($array, $i);
}
print microtime(true) - $t;
echo "\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    $array[] = $i;
}
print microtime(true) - $t;

echo "\n\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    $array[] = $i;
}
print microtime(true) - $t;
echo "\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    array_push($array, $i);
}
print microtime(true) - $t;

echo "\n\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    $array[] = $i;
}
print microtime(true) - $t;
echo "\n";
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    array_push($array, $i);
}
print microtime(true) - $t;

Jobs

php

<?php 
$jobs = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
$feed = array('F1', 'F2', 'F3');

$i=1;

$j=0;

foreach($jobs as $job) {
  echo $job.'----';
  if($i % 2 == 0 ) {
      if($j< count($feed)) {
        echo $feed[$j].'----';
        $j++;
      }
  }
  $i++;
}

?>

Testing

php

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php 
$jobs = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
$feed = array('F1', 'F2', 'F3', 'F4');

$i=1;

$j=1;

foreach($jobs as $job) {
  echo $job.'----';
  if($i % 2 == 0 ) {
      echo $feed[$j].'----';
      $j++;
  }
  $i++;
}

?>
</body>
</html>

1 2 3 4 5 6 7 ... 103 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.