Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.
Related
I am new to working with PHP sessions.
To test code using $_SESSION I have created 2 test pages(test_session.php, get_session_test.php). I have written this code using articles on w3schools https://www.w3schools.com/PhP/php_sessions.asp
The session variables are correctly set, I have used print_r($_SESSION) on the test_session.php page which shows they have been set as in the code. However when I view the second page get_session_test.php no session variables are shown. I have even tried the print_r($_SESSION) and this only shows an empty array: Array ()
in test_session.php the print_r shows [favcolor] => green [favanimal] => cat but in get_session_test.php no data shows
Here is the code on both pages:
test_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="cat";
echo "Session variables are set";
print_r($_SESSION);
echo '<BR><BR>';
echo $_SESSION["session_id"];
?>
</body>
</html>
get_session_test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
echo '<BR>';
echo '<BR>';
echo 'Favourite color is'.$_SESSION["favcolor"].'<BR>';
echo 'Favourite color is'.$_SESSION["favanimal"].'<BR>';
?>
</body>
</html>
My website has a very simple structure base on include in php.
Here the structure:
<?
session_start();
include("lang/".$_SESSION['lang'].".php");
?>
<html>
<body>
...
<? include("menu.php"); ?>
...
</body>
</html>
Content of menu.php is:
<div>
My name is <?= $_SESSION['name']; ?>
</div>
My problem is in the menu.php, the $_SESSION don't work anymore.
Is there a reason for that ?
Thanks.
You are not printing the name. That is why it is not shown.
Try this
<div>
My name is <?php echo "$_SESSION['name']"; ?>
</div>
I'm trying to execute a function in a file called 'function.php' into <?php ?> with a require_once, in HTML lines..
accueil.php (C:\wamp\www\e-commerce\MusicStore\accueil.php) :
<?php
require_once('http://localhost/e-commerce/MusicStore/templates/function.php');
?>
<!DOCTYPE html>
<html>
<body>
<?php
get_header();
?>
</body>
....
</html>
and function.php (C:\wamp\www\e-commerce\MusicStore\templates\function.php ):
<?php
function get_header()
{
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
?>
I got " Fatal error: Call to undefined function get_header() ".
But if I put the echo outside of the function, it works.. This is only into the function It doesn't work.
Does anybody know how to resolve this? I'm trying to clear my website with adding PHP function to structure my html page.
EDIT : The double '{' is a mistake on Stackoverflow but isn't in the file.
you pls try this
<?php
require_once('templates/function.php');
?>
and replace function.php because it has extra{
<?php
function get_header()
{
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
?>
Function has 2 opening braces {
Here is the fixed code:
function get_header() {
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
I am assuming folder location of your accueil.php is same as function.php
<?php
require_once('function.php');
?>
If its different then try relative path. If you can tell about locations of your both files you will get better help here.
The better approach is:
function get_header() {
{
return "your code here";
}
echo get_header();
The function is not a very good place to echo from
Can some one tell me how to "include" a variable from another .php file without all its other content.
index.php
<?php
$info=file('somedir/somefile.php');
$v1=trim($info[2]);
$v2=trim($info[3]);
$v3=trim($info[4]);
?>
the somedir/somefile.php
<?php
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
All the other content there may not be runned or showed.
?>
Can anybody please help me??
Edit:
Its for my dynamic page.
<html>
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
//$url = basename($url);
$info=file('sites/'.$url.'.php');
$title=trim($info[2]);
?>
<head>
<meta charset="UTF-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
include_once '/var/www/html/sites/'.$url.'.php';
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
</html>
Hope you understand my question now.
Programming is just driving your thoughts :)
So what i want to say that your question is how you can include just some part of an included file and my answer is that you can achieve that by doing a test each time the main file is included from withing this file to see if the file is included internally or not and you can be more precise in a way that you split your main file into block which are loaded due suitable variable
Take a look for this workaround and hope you will understand what i mean
Supposing we have the main file named main.php contains that contents
<?php
echo 'I am a java programmer';
echo 'I know also PHP very well';
echo 'When the jquery is my preferred toast !';
?>
now i have three external files that will include that file each file is specific for one of this 3 programming language
So i will create my 3 files in this way :
File : java.php
<?php
$iamjavadevelopper = 1;
include_once("main.php");
?>
File : phpfav.php
<?php
$iamphpdevelopper = 1;
include_once("main.php");
?>
File : jquery.php
<?php
$iamjquerydevelopper = 1;
include_once("main.php");
?>
and my main.php will be coded in this way
<?php
if(isset($iamjavadevelopper))
echo 'I am a java programmer';
if(isset($iamphpdevelopper))
echo 'I know also PHP very well';
if(isset($iamjquerydevelopper))
echo 'When the jquery is my preferred toast !';
?>
By this way each one of our three external files will show just a part of the included file :)
The only way I can think of without cookies or session's is to make an if condition in the page.
like that:
index.php
<?php include('somedir/somefile.php');?>
the somedir/somefile.php
<?php
if ($pageName != 'somefile.php') {
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
} else {
// All the other content
}
?>
Save the variables in a separate file that can be included separately. Do it the sane way. Structure your code properly, don't try to invent solutions for problems you have because your structure is messy.
I am new to PHP and cannot figure out why this is not working properly. What I want to happen is if a session is found execute the web page otherwise redirect to login.php. What is happening is the webpage is being executed and the user sees sql errors then the redirect happens. It's as if both the if and the else are being executed. Please help.
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>
a nicer way:
<?php
session_start();
if (!isset($_SESSION['userID'])){
header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);
?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
check your braces and do session_start before using the session
If you are using if else statements within a webpage you may find this format easier:
<?php if(true): ?>
HTML Here
<?php else: ?>
HTML Here
<?php endif; ?>
But the reason why your code isn't working is because you have got your curly braces mixed up.
Examine this example and compare it with your code:
if(true) {
// Do stuff
}
else {
// Do stuff
}
Try this code your first curtly bracket was closing instead of opening
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>