Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PHP 7 Error Handling

php

<?php
   interface Logger {
      public function log(string $msg);
   }

   class Application {
      private $logger;

      public function getLogger(): Logger {
         return $this->logger;
      }

      public function setLogger(Logger $logger) {
         $this->logger = $logger;
      }  
   }

   $app = new Application;
   $app->setLogger(new class implements Logger {
      public function log(string $msg) {
         print($msg);
      }
   });

   $app->getLogger()->log("My first Log Message");
?>

test

php

<?php
   interface Logger {
      public function log(string $msg);
   }

   class Application {
      private $logger;

      public function getLogger(): Logger {
         return $this->logger;
      }

      public function setLogger(Logger $logger) {
         $this->logger = $logger;
      }  
   }

   $app = new Application;
   $app->setLogger(new class implements Logger {
      public function log(string $msg) {
         print($msg);
      }
   });

   $app->getLogger()->log("My first Log Message");
?>

Execute PHP Online

php

<?php
$ar = array(
  array(
    "picTitle" => "标题2",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-26 11:59:50",
    "pictureurl" => "attachment/picture/uploadify/20141126/547550278b7db.jpg",
  ),
  array(
    "picTitle" => "标题2",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-26 11:59:50",
    "pictureurl" => "attachment/picture/uploadify/20141126/54755027ab89b.jpg",
  ),
  array(
    "picTitle" => "标题2",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-26 11:59:50",
    "pictureurl" => "attachment/picture/uploadify/20141126/547550273b753.jpg",
  ),
  array(
    "picTitle" => "标题2",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-26 11:59:50",
    "pictureurl" => "attachment/picture/uploadify/20141126/54755027d8488.jpg",
  ),
  array(
    "picTitle" => "同步写入信息和附件表里",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-20 16:05:16",
    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",
  ),
  array(
    "picTitle" => "同步写入信息和附件表里",
    "picCategroy" => "海报",
    "picAuthor" => "星耀学园",
    "picPostTime" => "2014-11-20 16:05:16",
    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",
  ),
);
$res = array();
foreach($ar as $item) {
  if(! isset($res[$item['picTitle']])) 
  {
      $res[$item['picTitle']] = $item;
      
  }
  else {
      $res[$item['picTitle']]['pictureurl'] .= ',' . $item['pictureurl'];
      
  }
}
var_export(array_values($res));
?>

Tester

php

<?php

// inputs
$iSearchQuery = "a";


// TODO: check input


// constants
$cColors = array(
    0 => "white",
    1 => "gray",
    2 => "black",
    3 => "red",
    4 => "green",
    5 => "blue",
    6 => "yellow",
    7 => "pink",
    8 => "brown",
    9 => "orange",
    10 => "cyan"
);

$cHex = array(
    0 => "0xffffff",
    1 => "0x888888",
    2 => "0x000000",
    3 => "0xff0000",
    4 => "0x00ff00",
    5 => "0x0000ff",
    6 => "yellow",
    7 => "pink",
    8 => "brown",
    9 => "orange",
    10 => "cyan"
);


// members
$mSearchResults1 = array();
$mSearchResults2 = array();

// find search query in array
foreach ($cColors as &$col)
{
    if (strpos($col, $iSearchQuery) !== false)
    {
        //echo $col . PHP_EOL;
        $mSearchResults1[] = $col;
        
        $colorKeyIndex = array_search($col, $cColors);
        $mSearchResults2[] = $cHex[$colorKeyIndex];
    }
}

// break the reference with the last element
unset($col);




echo json_encode($mSearchResults1, JSON_FORCE_OBJECT) . PHP_EOL;
echo json_encode($mSearchResults2, JSON_FORCE_OBJECT) . PHP_EOL;

$result = array_combine($mSearchResults1, $mSearchResults2);
print_r($result);
echo json_encode($result, JSON_FORCE_OBJECT) . PHP_EOL;

?>

Execute PHP Online

php

<?php
   class A {
      function A() {
         print('Style Constructor');
      }
   }
?>

Live_key_generator

php

<?php

 
$orderNo = 'CSTM_3GU1JUMQUTL';
$apiKey='?xn6dZdr';
$status='live';
$data=$orderNo.'|'.$apiKey.'|'.$status;
$hashKey=hash('sha512', $data);
echo $hashKey;

?>

PHP 7 Integer Division

php

<?php
   $value = intdiv(10,3);
   var_dump($value);
   print(" ");
   print($value);
?>

PHP 7 Error Handling

php

<?php
   class MathOperations {
      protected $n = 10;

      // Try to get the Division by Zero error object and display as Exception
      public function doOperation(): string {
         try {
            $value = $this->n % 0;
            return $value;
         } catch (DivisionByZeroError $e) {
            return $e->getMessage();
         }
      }
   }

   $mathOperationsObj = new MathOperations();
   print($mathOperationsObj->doOperation());
?>

PHP7 assert()

php

<?php
   ini_set('assert.exception', 1);

   class CustomError extends AssertionError {}

   assert(false, new CustomError('Custom Error Message!'));
?>

PHP7 random_int()

php

<?php
   print(random_int(100, 999));
   print("");
   print(random_int(-1000, 0));
?>

Advertisements
Loading...

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