Well the short answer is: It's not that PHP is turning bad to whole or you have to use another function, it's that your code is badly thought out. Javasctipt is a CLIENT, PHP is a Server. That variable will never reach the server, and now I show you also because you think that the variable is in php but in reality it is not.
Now let's see what your code is doing exactly.
The problem is that the line
$stemp = "<script> document.write(temp) </script>";
You are assigning the String document.write (temp), I literally recommend (it CAN NEVER BE CONVERTED TO ENTIRE THAT TEXT, IT DOES NOT MATTER WHAT FUNCTION OF PHP USES) use var_dump to analyze this type of situations.
And if you do echo of that variable you think I'm crazy and this is not true because you can read the value of the temperature but in reality this happens because: echo is printing code in browser (CLIENT) and the browser already has the variable of Javascript and that's why you think your code goes when you're never really going to walk.
SOLUTION:
For the practical purposes since you propose that you want to make a demo in your school and do not want to get into Forms or AJAX, I recommend a simple solution that both CLIENT and SERVER can access and are the COOKIES.
The problem with this solution is that you have to add more Javascript code and you have to force a reload of the page, so that the cookie is saved in the first instance and PHP can read it in the next reload IF YOU WILL NOT ALWAYS BE READING A VALUE FROM THE COOKIE PREVIOUS TO THE NEW LOADING, SINCE EVERYTHING OCCURS ON THE SAME PAGE.
In short, I do not know if it would be easier to use AJAX, as I'll copy it as your code would be and you decide.
NOTE: Add the function createCookie and urlCharge.
CrearCookie describes itself in what it does.
urlCargar is to send a trivial parameter q = 1 so that we know that we come from a recharge made by us in order that the value of the cookie is saved and php can read it, this is when your PHP page does not contain parameters and if it already has some parameter it will DELETE it by returning the URL Clean without parameters so that the JS prompt is executed again, so add the load button again.
<Doctype html>
<html>
<head>
<title></title>
<script>
function crearCookie(nombre, valor, dias) {
var expira;
if (dias) {
var date = new Date();
date.setTime(date.getTime() + (dias * 24 * 60 * 60 * 1000));
expira = "; expires=" + date.toGMTString();
} else {
expira = "";
}
document.cookie = escape(nombre) + "=" + escape(valor) + expira + "; path=/";
}
function urlCargar() {
var url = window.location.href;
// si el indice de ? es mayor a -1 quiere decir que lo encontro
// ya tiene parametro
if (url.indexOf('?') > -1) {
url = url.split('?')[0];
console.log(url);
} else {
url += '?q=1'
}
return url;
}
function cargarDeNuevo() {
url = urlCargar();
window.location.href = url;
}
if (location.search.indexOf('q=') < 0) {
temp = parseInt(window.prompt('Write a temperature', ''));
crearCookie("temperatura", temp, 2);
url = urlCargar();
window.location.href = url;
}
</script>
</head>
<body>
<?php
$phptemp=( int)$_COOKIE[ "temperatura"];
echo "The value of \$phptemp is : $phptemp <br>";
if (!is_nan($phptemp))
{
if ($phptemp < 0 || $phptemp> 38)
{
echo "Don't go outside, better stay in house today!!!.";
}
else
{
echo "You can go outside, it's a nice day.";
}
}
?>
<br>
<button onclick="cargarDeNuevo()">Cargar de nuevo</button>
</body>
</html>