How to check if variable is null or empty in substr with PHP

3 Answers

0 votes
$string = "php programming";

$result = substr($string ? $string : "***", 0,  3);

echo $result;




/*
run:

php

*/

 



answered May 25, 2022 by avibootz
0 votes
$string = "";

$result = substr($string ? $string : "***", 0,  3);

echo $result;




/*
run:

***

*/

 



answered May 25, 2022 by avibootz
0 votes
$string = null;

$result = substr($string ? $string : "***", 0,  3);

echo $result;




/*
run:

***

*/

 



answered May 25, 2022 by avibootz
...