Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 26, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/9d5d94b59f6eb0bb2552b125deef2eb1 to your computer and use it in GitHub Desktop.
Save parzibyte/9d5d94b59f6eb0bb2552b125deef2eb1 to your computer and use it in GitHub Desktop.
<html>
<?php
// Esta variable puede venir de cualquier lugar
$datos = [
"nombre" => "Luis Cabrera Benito",
"videojuegos_favoritos" => ["Resident Evil 2", "Celeste", "Cuphead", "Donkey Kong"],
"edad" => 22,
"mascota" => [
"nombre" => "Maggie"
]
];
// Aquí más código...
?>
<head>
<title>PHP Test</title>
<script type="text/javascript">
const datos = <?php echo json_encode($datos); ?>
// Justo aquí estamos pasando la variable ----^
console.log("Los datos son: ", datos);
// Y podemos usar los datos normalmente ;)
const nombre = datos.nombre;
const videojuegos_favoritos = datos.videojuegos_favoritos;
videojuegos_favoritos.forEach(videojuego => console.log("A %s le gusta jugar %s", nombre, videojuego));
</script>
</head>
<body>
<p>Parzibyte.me</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment