0

I know one can likely use the switch statement but I have this currently to check that all function parameters that are supposed to be an int are and if not show an error:

if(!is_int($chartWidth)){
   echo "Chart width must be an integer";
}
if(!is_int($chartHeight)){
    echo "Chart height must be an integer";
}
if(!is_int($gridTop)){
    echo "Grid top must be an integer";
}
if(!is_int($gridLeft)){
    echo "Grid left must be an integer";
}

Can this be coded any more efficiently or shorter?

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
user389391
  • 97
  • 1
  • 8

4 Answers4

3

if these are function parameters as you say, since PHP 5 you have been able to use type hinting for functions. Assuming you expect a value every time you can do the following.

function chart(int $width, int $height, int $top, int $left) {
    // Some code
}

If a value passed to the function is a type which is NOT an integer, you will get a fatal error.

MHewison
  • 846
  • 10
  • 17
  • Error depends on `declare(strict_types)`. – u_mulder Dec 01 '21 at 20:25
  • @u_mulder to be exact, [as of php8](https://wiki.php.net/rfc/consistent_type_errors), passing a parameter of illegal type results in a TypeError, regardless of strict_types option. – berend Dec 01 '21 at 20:37
  • @berend - PHP will still coerce things like `foo('1')` and `foo(1.5)` for you unless you use strict types for the paged script. – Jared Farrish Dec 01 '21 at 20:47
  • Scalar type hints, and return type declarations, were not available until PHP 7. – Jared Farrish Dec 01 '21 at 20:54
  • @MHewison I see this as the best option as it accomplishes what I intended, it does not adds not extra code and why did i not think of it lol UNFORTUNATLEY it appears I forgot that I am not running PHP 8 yet :-( So my code will have to wait for 8. – user389391 Dec 01 '21 at 22:51
  • @MHewison I will mark it a the answer for me as I will be using your suggestion in a few next years when we move to 8. thx – user389391 Dec 01 '21 at 22:58
  • You need at least PHP 7. – Jared Farrish Dec 02 '21 at 00:50
1

You could use a function:

function assertInt($value, $name)
{
    echo is_int($value) ? '' : $name . ' must be an integer.<br>';
}

assertInt($chartWidth,  'Chart width');
assertInt($chartHeight, 'Chart height');
assertInt($gridTop,     'Grid top');
assertInt($gridLeft,    'Grid left');

I don't like the echo inside the function, what if you have multiple variables that are not integers? I therefore added the <br> at the end.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
0

It is possible with dynamic variable names.

$chartWidth = 100;
$chartHeight = 200;
$gridTop = '1,2';
$gridLeft = 1.2;

$arr = [
  'chartWidth' => "Chart width must be an integer",
  'chartHeight' => "Chart width must be an integer",
  'gridTop' => 'Grid top must be an integer',
  'gridLeft' => 'Grid left must be an integer'
];

$result = [];
foreach($arr as $k => $v) {
    if(!is_int(${$k})){
        $result[] = $v;
    }
}

print_r($result);

output

Array
(
    [0] => Grid top must be an intege
    [1] => Grid left must be an integer
)
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
0

This will iterate each variable and if you want to stop or print something before iterate the rest of variables i think it will be the best

 $data = array($chartWidth,$chartHeight,$gridTop,$gridLeft);
    
    foreach ($data as $value) {
        echo gettype($value) , "\n";
      if(gettype($value)!="integer"){
         // do something
       }
    }
Mostafa Ezzat
  • 29
  • 1
  • 11