is_countable check if something is countable.

When counting uncountable objects,you get warning. The is_countable function can help prevent this warning.

Note This is New feature in PHP 7.3 not exist in php 7.2 or lower version.

bool is_countable ( mixed $var )
$count = is_countable($variable) ? count($variable) : null;

Example

var_dump(is_countable([1, 2, 3])); // bool(true)
var_dump(is_countable(new stdClass())); // bool(false)

/* *************************************** */

array_key_first and array_key_last

These functions allow you to get the first and last key respectively.

Note This is New feature in PHP 7.3 not exist in php 7.2 or lower version.

Example

$array = ['a' => 1, 'b' => 2, 'c' => 3];

$firstKey = array_key_first($array); // 'a'
$lastKey = array_key_last($array);  // 'c'

assert($firstKey === 'a');  // no output because its true
assert($lastKey === 'c');  // //no output because its true

for an empty array the functions return null:

$array = [];

$firstKey = array_key_first($array);  // null
$lastKey = array_key_last($array);  // null

/* *************************************** */

assert()

The assert() function is a good way to ensure certain conditions are true throughout the life of your code. Assertions should be used as a debugging feature only. it added from php 5.4

<?php
    print "Stage 1\n";
    assert(1 == 1);
    print "Stage 2\n";
    assert(1 == 2);
    print "Stage 3\n";
   
   $x = 10;
    $y = 11;
    assert($x > $y);

/* 
Stage 1
Stage 2
Warning: assert(): assert(1 == 2) failed in Standard input code on line 5
Stage 3
Warning: assert(): assert($x > $y) failed in Standard input code on line 10
*/

Note that the first assert() is not seen in the output at all because it evaluated to true, whereas the second assert() evaluated to false, so we get a warning about an assertion failure. Also note that we see "Stage 3" after the assertion failure warning, because the script carries on executing after the failure.

/* *************************************** */

JSON_THROW_ON_ERROR

provides a new way of handling JSON errors. change the error behaviour of json_decode() and json_encode().

Currently, Json_encode() will throw only false error whereas json_decode will throw null, and null can be true value.

JSON_THROW_ON_ERROR in both functions which ignores the global error state.

json_encode($data, JSON_THROW_ON_ERROR);
json_decode("invalid json", null, 512, JSON_THROW_ON_ERROR);

// Throws JsonException

/* *************************************** */

Trailing commas in function calls

Trailing commas (or “final commas”) are commas appended to a list of elements, trailing commas in function calls will be valid syntax.

unset(
    $one,
    $two,
    $three,
);
$colors = [
  'blue',
  'red',
  'green',
];
return view('posts.show', compact(
    'post',
    'author',
    'comments',
));

#php_new_features

PHP#PHP

Comments (1)

Mohamed Samir
Mohamed Samir
8 months ago

usefully, thank you.