What you are trying to do above won't work because php code is processed on the server-side (before browser gets the page content) and javascript code on client-side (after the php script processed and page downloaded by the browser).
So you have to find a way to check if javascript is enabled within your php script, and here it is
Servers side JavaScript detection in PHP4
EDIT: I thought you want to detect javascript at first, but now I see you need something different. Well you can do what you need through an XMLHttpRequest (AJAX). A simple AJAX tutorial can be found at:
AJAX Tutorial
And here is more to show why the code above won't work:
-
<?php
-
$city='London';
-
?>
-
<html>
-
<head></head>
-
<body>
-
<script language="javascript">
-
if (0) {
-
<?php
-
$city='Paris';
-
?>
-
}
-
</script>
-
<?php
-
echo $city;
-
?>
-
</body>
-
</html>
-
Browser always displays 'Paris' because php code is already processed and browser just gets the resulting page.
IMPORTANT NOTE: Using AJAX or any other javascript-based validations/tests does not guarantee that your php script cannot be run without passing the validation/test. You can only secure your test through server-side validations.