Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: yina07 en 30 Enero 2019, 04:20 am



Título: archivo con include y require
Publicado por: yina07 en 30 Enero 2019, 04:20 am
Saludos! necesito ayuda con unos ejercicios con include y require, como puedo hacer el siguiente ejercicio en php para que salgan las variables del otro archivo en el body del index.php y lo del segundo ejercicio como se hace?

Incluye el archivo ( lib/include.php ) en el archivo ( index.php ) e imprime las variables en el ( body ) del archivo ( index.php )

archivo index.php:

Código
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title>Include y require en PHP</title>
  11. </head>
  12. <body>
  13.  
  14.  
  15.  
  16.  
  17.  
  18. </body>
  19. </html>

archivo lib/include.php:

Código
  1. <?php
  2.  
  3. $funciones = ['isset', 'unset', 'file_exists', 'get_file_contents', 'in_array', 'is_array'];
  4.  
  5. $nombre = "PHP & MySQL";
  6.  
  7. $cantidad = 15.69;
  8.  
  9. ?>

ejercicio 2
Require el archivo ( lib/require.php ) en el archivo ( index.php ) y convierte el retorno del archivo en array e imprímelo en el ( body ) del archivo ( index.php )

index.php:

Código
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title>Include y require en PHP</title>
  11. </head>
  12. <body>
  13.  
  14.  
  15.  
  16.  
  17.  
  18. </body>
  19. </html>

archivo lib/require.php

Código
  1. <?php
  2.  
  3. return '{
  4. "nombre" : "SDQ Training Center",
  5. "cursos" : [
  6. "PHP & MySQL",
  7. "AutoCAD",
  8. "Revit",
  9. "HTML 5 & CSS 3",
  10. "Java"
  11. ],
  12. "fecha" : "2009-05-10"
  13. }';
  14.  
  15. ?>





Título: Re: archivo con include y require
Publicado por: Robocop8 en 31 Enero 2019, 15:12 pm
Código
  1. <?php
  2.  
  3. include_once "lib/include.php"
  4.  
  5.  
  6.  
  7. ?>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>Include y require en PHP</title>
  12. </head>
  13. <body>
  14.  
  15. <?php
  16.  
  17. echo "<h1>include</h1>";
  18. echo "<h2>Variable #1 </h2>";
  19. var_dump ($funciones);
  20.  
  21.  
  22. echo "<h2>Variable #2 </h2>";
  23. var_dump ($nombre);
  24.  
  25.  
  26. echo "<h2>Variable #3 </h2>";
  27. var_dump ($cantidad);
  28.  
  29.  
  30.  
  31.  
  32. ?>
  33.  
  34.  
  35.  
  36. </body>
  37. </html>


el segundo ejercicio no se hacerlo. Saludos!


Título: Re: archivo con include y require
Publicado por: #!drvy en 31 Enero 2019, 16:13 pm
Cuando usas un return fuera de un scope, haces que PHP devuelva ese contenido si hay referencia previa. Básicamente, el include/require pasa a tener el valor del archivo que se ha incluido.


Código
  1. <?php
  2.  
  3. $patata = require_once 'lib/require.php';
  4.  
  5.  
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title>Include y require en PHP</title>
  11. </head>
  12. <body>
  13.  
  14.  
  15. <?php var_dump($patata); ?>
  16.  
  17.  
  18. </body>
  19. </html>


Saludos