-2

In first.php i have $name = "Josh". How to echo $name in secound.php. Both are in same folder.

In first.php

$name = "Josh";

In secound.php

echo $name;

Notice: Undefined variable: name in D:\XAMPP\htdocs\Folder\secound.php on line 2

Edit

include 'first.php';

echo $name;
chris85
  • 23,846
  • 7
  • 34
  • 51
Lazar
  • 47
  • 1
  • 7
  • 2
    The PHP interpreter is top-bottom, so if you don't have $name instantiated before the call, will never work. Include the first script before your call of $name and will be good. – capcj May 14 '17 at 21:27
  • i did it. but i have some html code and it's include that html – Lazar May 14 '17 at 21:30
  • So separate the html code from the logic of the application with functions and get the name value after that as you wish. If you put the content of both php here will help A LOT. – capcj May 14 '17 at 21:31

1 Answers1

1

To access the data in the second file, you'll need to include it.

At the top of your secound.php script, add this code:

include 'first.php';

You could also use require, like this:

require 'first.php';

The difference between the two can be found in the PHP manual:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

xcvd
  • 666
  • 1
  • 5
  • 13