Strange throw error instead of proper error generated

I am using this

$bpm = isset($_GET['bpm'])  ? $_GET['bpm']
    : throw new \InvalidArgumentException('value not set bpm.');

but get sent back Parse error : syntax error, unexpected ‘throw’ (T_THROW) in /home/vol1_2/epizy.com/epiz_33087480/htdocs/add_track.php on line 36 instead of my error returned . Can anyone help ?

You can’t assign throw to a variable. Try this instead.

isset($_GET['bpm']) ? $bpm = $_GET['bpm'] : throw new \InvalidArgumentException('value not set bpm.');
4 Likes

Thanks Wacky for your help :slight_smile: nice name it didn’t like the ? or : throwing exceptions for both I thought you could do this in php 8.0

tried $bpm = isset($_GET[‘bpm’]) : throw new \InvalidArgumentException(‘value not set bpm.’);

Got error Parse error : syntax error, unexpected ‘:’ in /home/vol1_2/epizy.com/epiz_33087480/htdocs/add_track.php on line 34

hugs
D

I don’t know about how this works with PHP 8.0 (I don’t like throwing stuff from a ternary operator, but that’s just my opinion), but our hosting is still running on PHP 7.4, which means PHP 8 specific language features are not available yet.

We hope to be able to upgrade to PHP 8 soon.

4 Likes

Ok thank you Admin i thought it might be on 8.0 :slight_smile: why I was getting lots of shake down problems. I don’t think PHPAdmin is latest version either just fyi.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.