class ClickWithContent
{
public $LeftUser;
public function y()
{
$value = match(1) {
1 => 'Hii..', // <-------- syntax error, unexpected '=>' (T_DOUBLE_ARROW)
2 => 'Hello..',
default => 'Match not found !',
};
}
}
On the above code line I tried to use match function instead of switch statement. But I get following error on Line mentioned on the code.
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
CodePudding user response:
PHP Match
match()
expression according to php.net, the docs state:
Match expressions are available as of PHP 8.0.0.
Apparently you are using php version < 8.0.0. make sure you are using PHP 8 , you can try checking your php version using phpinfo()
inside your code or php -v
on your terminal.
Alternate
An alternative for match()
is, (or should I say match()
is an alternative of) what we call switch()
you can simply replicate what you are trying here by using switch()
(not to forget that switches don't work with integers because 0 will simply evaluate as false and vice versa) so you can try something like this:
$target = 1;
$value = null;
switch((string) ($target)) // typecast to string
{
case "1":
$value = 'Hi..';
break;
case "2":
$value = 'Hello..'
break;
default:
$value = 'Match not found !'
}
I hope that explains your problem.