<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>if else in php</title> </head> <body> <script> var a = 123; //to send the prompt value to php, reference taken from stack overflow </script> <?php $a = "<script>document.writeln(a)</script>"; echo $a; ?> </body> </html>en el código anterior, obtuve el valor de la variable javascript 'a' en PHP, lo obtuve con éxito en la variable $a e intenté imprimirlo y luego también se imprimió correctamente. Como $a contendría el número '123' en la cadena, traté de convertirlo en un número entero usando la función intval() pero siempre me muestra el valor de $a como 0 cuando lo hago eco. el siguiente código describe lo mismo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>if else in php</title> </head> <body> <script> var a = 123; //to send the prompt value to php, reference taken from stack overflow </script> <?php $a = "<script>document.writeln(a)</script>"; $a = intval($a); echo $a; ?> </body> </html>Tienes que enviarlo al servidor para procesarlo. Aquí un ejemplo de trabajo.
<?php $canDrive = false; // use GET to retrieve the value of the "age" query string if(isset($_GET["age"]) && $_GET["age"] >= 18) $canDrive = true; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>if else in php</title> </head> <body> <!-- Here goes your code --> <?php if ($canDrive) { echo "You can drive"; } else { echo "You can't drive"; } ?> <form action="" method="get"> <input type="text" name="age" id=""> <input type="submit" value="submit"> </form> </body> </html>Recuerde, JavaScript y PHP no pueden comunicarse directamente.