Affan
Affan

Reputation: 151

If else is not working properly in my php code

I have a problem in my if else condition. My Screen Width is:1366 My code should show the if part but always it is showing the else part. I don't know WHY.. First of all i run the if condition without casting but problem was same... Then later on i casted into (int) but still the problem is same

$screenWidth = "<script>document.writeln(screen.width);</script>";
echo "Screen Width is:" . $screenWidth;
if( (int)$screenWidth > 500) 
{ 
    echo "if part:" . $screenWidth;
}
else 
{ 
    echo "else part:" . $screenWidth;
}

Upvotes: 0

Views: 57

Answers (1)

larsAnders
larsAnders

Reputation: 3813

You really can't do this with PHP. Check out this question and answer. However, you can use Javascript to accomplish what you're after. It just requires a destination div to print the results.

<div id="output">
</div>

<script>
var screenWidth = screen.width;
if(screenWidth > 500){ 
    document.getElementById('output').innerHTML = "if part:" + screenWidth;
} else { 
    document.getElementById('output').innerHTML = "else part:" + screenWidth;
}
</script>

Upvotes: 1

Related Questions