Echo variable in more files [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
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;

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.

Related

how do I make a php variable successfully available in another php file? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
I have 2 php files inside joomla and I want to make a variable from php-file 1 available in php-file 2.
php-file_1.php
$a_variable = $input->getString('text');
php-file_2.php
include(php-file_1.php);
echo $a_variable;
I get Notice: Undefined variable: a_variable in /var/www/vhosts/a_domain.de/httpdocs/php-file_2.php on line 2
what's wrong here?
include and require are not functions, they are language constructs. Therefore they need to be used without brackets.
Either way, you missed quotes around php-file_1.php.
include 'php-file_1.php'; should work fine.

PHP Error: Function name must be a string in [duplicate]

This question already has answers here:
PHP Error: Function name must be a string
(8 answers)
Closed 6 years ago.
I added following few lines on top of my PHP code, but it is giving error
the error is
Function name must be a string in
/home/developeriq/public_html/doylesweb/hotel_search.php on line 3
if(!isset($_session)) {
$session_start();}
Your code is wrong:
if(!isset($_session)) {
$session_start();}
$session_start() should be session_start(). $ is for variables, session_start() is a function, not a variable, thus, $ is not required.
Also, it must be capitalised as it's a superglobal array. (Credits to #AkshayPrakash for pointing out)
if(!isset($_SESSION)) {
session_start();}
Documentation: http://php.net/manual/en/reserved.variables.session.php

How to pass variable in PHP using include? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
I have one page where i need to send some results to another PHP file.
include("test.php");
but i want to pass variable (var) also so that which can be processed by test.php using $_GET['var'] command. How is it possible in PHP?
$var1="hello";
include ("test.php");
the test file contains below code
echo $var1." world";

Variable from required document not found [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
MySQLiconfig.php:
<?php
$MySQLi = new mysqli('localhost','root','123','Database') or die('ERROR');
Other document:
<?php
require 'MySQLiconfig.php';
function DoAQuery($Query){
$MySQLi->query($Query);
}
The error is:
Undefined variable: MySQLi
You are wrong with variable scope. In the function you need to use like global $MySQLi or you can pass as function parameter.

Is seeing Notice in the HTML of any concern for PHP? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I went to view page source and seen this in the HTML for a select drop-down list that is populated by a query:
Notice: Constant DB_USER already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 8
Notice: Constant DB_PASSWORD already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 9
Notice: Constant DB_HOST already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 10
Notice: Constant DB_NAME already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 11
Should I be concerned about this? And if so, what should I be looking for in the PHP?
Thanks!
Yes, you should always worry over notices being generated by your code. PHP is trying to tell you that it thinks you've done something wrong, but can't be sure what it is.
In your case, I'm assuming you're using require instead of require_once.
You should always strive to write code that produces zero errors including notices. This will help you prevent unexpected behavior and make sure PHP is performant.
In your case you either are including a file multiple times or are trying to define a constant multiple times. In the first case you need to switch from include()/require() to include_once()/require_once(). In the latter you need to check if the constant is defined before trying to define it again.
defined('DB_USER') || define('DB_USER', 'your value');

Categories