require_once of function file in a header - php

ive got an global include/header.php file like:
UPDATE
folder structure :
/
include/
header.php
functions.php
content/
show.php
include/header.php
<?php
require_once('functions.php');
$settings = blaa;
....
?>
include/functions.php
<?php
function hello()
{
echo "hello world";
}
?>
and now a content file like content/show.php
content/show.php
<?php
require_once('../include/header.php');
echo "show page want to say: ";
hello();
?>
and now if i look in the apache error_log call to undefined function in content/show.php on line...
i cannot find out why :-/
Greetings

If you tweak your header.php code to this:
<?php
define('INCDIR', str_replace('\\', '/', dirname(__FILE__)));
require_once(INCDIR . '/functions.php');
It wont matter where you include it from it will constantly find the right path to the include folder

Related

Call function in includes folder that have php file into another php file - Wordpress Plugin

This is about the WordPress plugin.
I look at other people WordPress plugin has somewhat like this code:
example.php
<?php
$display = get_option( 'display' );
if(!defined('PLUGIN_DIR'))
define('PLUGIN_DIR', dirname(__FILE__));
function init() {
load_plugin_textdomain('example', false, basename( dirname( __FILE__ ) ) . '/lang' );
}
add_action('plugins_loaded', 'init');
include(PLUGIN_DIR . '/includes/display.php');
?>
and then there is includes a folder that has display.php
have code like this:
display.php
<?php
function display(){
//thecodes
}
?>
I've tried this coded concept. From what I analyze, if I'm not wrong, this code for calling functions in display.php, call the function display(){} code
this is my structure:
>includes
>>functions.php
achan.php
index.php
and, this is my code look like:
achan.php
<?php
$test = get_option('test');
define(PLUGIN_DIR . dirname(__FILE__));
include(PLUGIN_DIR . 'includes/functions.php');
function testing(){
global $test;
echo ($test);
}
add_action('bbp_theme_after_reply_author_details', 'testing');
?>
and in includes folder, have functions.php
my code like this:
functions.php
<?php
function test(){
echo "HELLO";
}
?>
But not working. Is there any wrong with my codes? the Hello not displayed at bbp_theme_after_reply_author_details. or maybe there is a more efficient way to call a function in another folder PHP file? Thank you, web developer.
You should call test somewhere in your code, Something like this:
<?php
$test = get_option('test');
define(PLUGIN_DIR . dirname(__FILE__));
include(PLUGIN_DIR . 'includes/functions.php');
function testing(){
global $test;
echo ($test);
// You should call test like this.
test();
}
add_action('bbp_theme_after_reply_author_details', 'testing');
?>
Look at test() in function testing()

PHP Fatal error: Call to undefined function?

So i have a problem with my website when im hosting it on my webhost.
i get this error:
PHP Fatal error: Call to undefined function getSkillIcons()
The weird thing is that locally(Xampp) it works just fine.
This is how i included the function(in index.php):
<?php include 'http://sub.website.com/incl/Settings.php'; ?>
this is where i call it (index.php):
<div class="panel-body" style="text-align:center">
<?php getSkillIcons(explode(',', skills)); ?>
</div>
This how i defined skills (settings.php)
define("skills", "Test1,Test2,Test3,Test4");
this is the function itself: (settings.php)
function getSkillIcons($skills) {
echo '<img src="http://sub.website.com/incl/img/skill_icons/Overall-icon.png" class="skill-icon">';
for ($i = 0; $i < count($skills); $i++) {
$tooltip = 'title="'.$skills[$i].'" data-toggle="tooltip" data-placement="top"';
echo '';
}
Call to undefined function error clearly shows that its not getting your function where you have defined. Reason is you are attaching full path of settings.php file with http.
You need to include settings.php file without http path at the top of 'index.php' and make sure settings.php file is located in your project only. If it is located at the same folder with index.php, then simply include like below.
<?php include __DIR__.'/settings.php'; ?>
If your settings.php file is located in some other folder then you can use $_SERVER['DOCUMENT_ROOT'] to include that file like below:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/settings.php";
include_once($path);
Also just an FYI for anyone trying to call a function within the same class in PHP, refer to the function like below:
return $this->doSomething($str);

Run some PHP code from other file

I have two files:
In a.php:
<?php echo "one"; include 'b.php'; echo "three"; ?>
In b.php:
<?php echo"one"; echo"two"; echo"three"; echo"four"; echo"five"; ?>
My question is how get only echo"two"; from b.php and what I must write in a.php?
To include other PHP files into another, add this to your code
include('otherfile.php');
You can't just retrieve a port of the code, including the file will execute the whole file. You could use function do this:
// File function.php
<?php
function a() {
echo 'one';
}
function b() {
echo 'two';
}
// File index.php
include('function.php');
a(); // will echo one
Here's a proper way to accomplish what you want.

Include and class PHP

Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.

require_once() function unable to include functions()

index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}
Now when i go to index.php, it gives me this error:-
Fatal error: Call to undefined function abspath() in C:\xampp\htdocs\index.php on line 3
Hey all my commenting people were right... i don't know why it happened but when i restarted my xampp it worked fine for me :)
So this is 100% correct
index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}

Categories