I am working on an example from a php book and getting an error on line 8 with this code
<?php $agent = getenv("HTTP_USER_AGENT"); if (preg_match("/MSIE/i", "$agent")); { $result = "You are using Microsoft Internet Explorer"; } else if (preg_match("/Mozilla/i", "$agent")); { $result = "You are using Mozilla firefox"; } else {$result = "you are using $agent"; } echo $result; ?>
Try:
$agent = getenv("HTTP_USER_AGENT"); if (preg_match("/MSIE/i", $agent)) { $result = "You are using Microsoft Internet Explorer"; } else if (preg_match("/Mozilla/i", $agent)) { $result = "You are using Mozilla firefox"; } else { $result = "you are using $agent"; } echo $result;
Two things:
You had a semicolon at the end of your if statements. This means that the subsequent opening bracket was a local block that always executes. This caused a problem because later you had an operator elsethat was not bound to the operator if; and
else
if
Execution is "$agent"not necessary and is not recommended. Just go to $agent.
"$agent"
$agent
At the end of the instructions ifthere ;.
;
The reason for the error:
if(...) ; { ... }
, if , .
if(...) ; { // blk A } else { ... }
Unexpected else, if, , , blk A, . , else, , if, . , statement (s):
Unexpected else
blk A
statement
if(...) ; do_something; else { ... }
remove the half-columns from the end of the lines with "if" in them.
Why do you have a semicolon here? if (preg_match("/MSIE/i", "$agent"));and hereelse if (preg_match("/Mozilla/i", "$agent"));
if (preg_match("/MSIE/i", "$agent"));
else if (preg_match("/Mozilla/i", "$agent"));
Source: https://habr.com/ru/post/1741202/More articles:Можно ли выполнить подсчет ссылок через композицию? - c++Problem creating PyGTK on CentOS - python- (void) keyboardWasShown не вызывается при переключении на другой UITextField - iphonePrepare for title tag using jQuery? - jqueryHow can you access batch information in a JpCap package - javaSHBrowseForFolder, но с текстовым полем для навигации - winapiHow to create a Turing machine that takes a decimal number with a number from 0 to 9 and displays a cube - turing-machinesDefault WPF DataGrid Column Types - wpfSending email through a proxy using gmail smtp - c #The explicit equivalence of Java Python. Got value:% s?%? - javaAll Articles