Posts

A variable variable in PHP

<?php $varname = 'tireqty'; $$varname = 100; echo $tireqty; // output: 100 ?>

Calling a function using variable name in PHP

<?php function show_name() {     echo 'Rangan Roy'; } $var = 'show_name'; $var(); // output: Rangan Roy ?>

Calling a function inside another function in PHP

<?php function name() {     return 'Rangan Roy'; } function show_name() {     echo name(); } show_name(); // output: Rangan Roy ?>

Echoing a function that echoes and returns values in PHP

<?php function print_name() {     echo 'Name';     return 'World'; } echo 'Hello'.print_name(); /* Output: Name Hello World */ ?>

Echoing a function that echoes something and returns nothing in PHP

<?php function print_name() {     echo 'Name'; } echo 'Hello'.print_name().' World.'; /* Output: Name Hello World */ ?>

Timestamps in PHP

<?php echo date('h:i:s a', time() + 5 * 3600).'<br/>'; // added 5 hours to current time echo date('h:i:s a', strtotime('5 hours')).'<br/>'; // added 5 hours to current time $timestamp = mktime(0, 40, 30, 2, 11, 95); // made own time echo 'Time: '.date('h:i:sa', $timestamp).'<br/>'; // Time: 12:40:30am echo 'Date: '.date('d/m/Y', $timestamp).'<br/>'; // Date: 11/02/1995 $timestamp = mktime(12, 40, 30, 2, 11, 95); echo 'Time: '.date('h:i:sa', $timestamp).'<br/>'; // Time: 12:40:30pm $timestamp = mktime(13, 40, 30, 2, 11, 95); echo 'Time: '.date('h:i:sa', $timestamp).'<br/>'; // Time: 01:40:30pm ?>

Rolling a dice in PHP

<?php if(isset($_POST['roll'])) {     echo 'You rolled a '.rand(1, 6).'<br/><br/>'; } ?> <form action="index.php" method="post">     <input type="submit" name="roll" value="Roll Dice"/> </form>

Replace every instance of a substring in PHP

<?php if(isset($_GET['submit'])) {     if(strlen($_GET['text']) && strlen($_GET['search_for']) && strlen($_GET['replace_with']))     {         $text = $_GET['text'];         $search_for = $_GET['search_for'];         $replace_with = $_GET['replace_with'];         $offset = 0;         $search_for_length = strlen($search_for);         $replace_with_length = strlen($replace_with);         $text = ' '.$text; // that's how search position will never be 0                 while($search_for_position = strpos($text, $search_for, $offset))         {             $text = substr_replace($text, $replace_with, $search_for_position, $search_for_length);             $offset = $search_for_position + $replace_with_length; // that's how $search_for will not be found in $replace_with when $search_for actualy exists in $replace_with         }                 echo $text.'<hr/>';     }     else     {    

How '0' becomes a false value in PHP

<?php if('0') {     echo 1; } else {     echo 0; } // outputs 0, because 'if' statement needs to convert '0' into boolean type value to work and in PHP (bool)'0' creates boolean false if('0' == false) // because of loose comparison '0' will be converted into boolean type automatically {     echo 1; } else {     echo 0; } //ouputs 1 if('0' === false) {     echo 1; } else {     echo 0; } // outputs 0, because '0' is a string after all and false is boolean, so their type didn't match here and for strict comparison no type conversion happened ?>