I'm using the following code to try and print the operating system of the user:

Header:

<?php
$user_agent = getenv("HTTP_USER_AGENT");

if (strpos($user_agent, "Win") !== FALSE)
$os = "Windows"; 
else (strpos($user_agent, "Mac") !== FALSE)
$os = "Mac";
?>

Body:

<?php
if($os = "Windows")
{

}
elseif($os == "Mac")
{

} 
?>

I get the error

Parse error: syntax error, unexpected '$os' (T_VARIABLE) in C:\xampp\xamppfile\htdocs\ProjectSite\includes\identifier.php on line 7

share
    
You can't put condition in else statement. – Rikesh Mar 11 '14 at 11:22
    
(a) Which is line 7? (b) How do you handle the case where the $user_agent is neither Mac nor Windows? That will lead to an undefined $os, won't it? – TRiG Mar 11 '14 at 11:27
up vote 0 down vote accepted
<?php
$user_agent = getenv("HTTP_USER_AGENT");

if (strpos($user_agent, "Win") !== FALSE) {
    $os = "Windows"; 

}
else if(strpos($user_agent, "Mac") !== FALSE) {
    $os = "Mac";
}

?>
share
    
this is not helpful unless you add some explanation of what the questioner was doing wrong and what your proposed code does – Our Man in Bananas Mar 11 '14 at 11:43

You cannot have condition in else statement should use else if and also have practice of declaring your variable before using it,

$os = "";
if (strpos($user_agent, "Win") !== FALSE)
$os = "Windows"; 
else if(strpos($user_agent, "Mac") !== FALSE)
$os = "Mac";
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.