PHP function not found with require_once in html - php

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

Related

Re-declaring a function in a child theme

P.S. Hi Admin, I tried looking for the solution but could not see the same issue anywhere!
Hi all,
I have a child theme. I have a function which is wrapped in if !function_exists as below. The file in is includes folder. The function is in wp-content/themes/themename/includes/alias-function.php
if (!function_exists('jem_render_buy_fee ')) {
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
</div>
<?php
}
}
}
Because it is in includes folders but wrapped in function_exists, so I declare the function in functions.php as below. This file is in wp-content/themes/themename-child/functions.php
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
</div>
<?php
}
}
I am getting the error:
Your PHP code changes were rolled back due to an error on line 1873 of file wp-content/themes/themename/includes/alias-function.php. Please fix and try saving again.
Cannot redeclare jem_render_buy_fee() (previously declared in wp-content/themes/themename-child/functions.php:215)
Why am I getting this? The error is wrapped in function_exist.
I can see its loading the function.php, but then it should ignore the existing one in the file.
Thank you in advance.
In your check for function existence, there's a space behind the function name.
!function_exists('jem_render_buy_fee ')
Without this space it should work fine if the order af calling code is correct.
First Call:
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
</div>
<?php
}
}
Then call:
<?php
if (!function_exists('jem_render_buy_fee')) { //Space removed from 'jem_render_buy_fee '
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if ($fee): ?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
</div>
<?php endif;
}
}

PHP syntax simplify

IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>

Accessing php variables at different places in the same file

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.

Web page is not available in Localhost

I am making a cms application and it was working fine until i made a new page called "session.php" and added a function into that page I then wrote the require once command into the main page of my application and when I run it says "Webpage is not available"...i remove the require once command and my application works again. Does anyone know how to fix this problem?
This is the code for session.php
<?php session_start(); function message() {
if (isset($_SESSION["message"])) {
$output = "<div class=\"message\">";
$output .= htmlentities($_SESSION["message"]);
$output .= "</div>";
return $output;
}}?>
This is the code for the page im trying to load
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php include("../includes/layouts/header.php"); ?>
<?php find_selected_page(); ?>
<div id="main">
<div id="navigation">
<?php echo navigation($current_subject, $current_page); ?>
<br />
+ Add a subject
</div>
<div id="page">
<?php echo message(); ?>
<?php if ($current_subject) { ?>
<h2>Manage Subject</h2>
Menu name: <?php echo $current_subject["menu_name"]; ?>
<?php } elseif ($current_page) { ?>
<h2>Manage Page</h2>
Menu name:<?php echo $current_page["menu_name"]; ?>
<?php } else { ?>
Please select a subject or a page.
<?php }?>
</div>
</div>
<?php include("../includes/layouts/footer.php"); ?>
The code posted looks oddly-formatted, but I'll venture to say it's not a syntax error.
As others stated, you need to learn the "debugging" process which includes monitoring the error.log file (assuming Apache) and the browser's console (where applicable).
During development, it's a good idea to turn on error reporting to make this a bit easier. Place the following lines at the top of your PHP scripts:
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
That won't display 100% of the issues (99.8% give or take), so read up on how to view your server's error log. The answer to your question is hidden in there, I guarantee it.

php include variables without including

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.

Categories