I have a functions.php page with some functions, it is required in the default.php page which is required by all my other pages (index.php) at the first line.
index.php (1st line after <?php
require("includes/default.php");
default.php
<?php
//mysql
require("templates.php");
require("functions.php");
?>
functions.php
<?php
function rating_format($num) {
if ($num > 0) {
return "+" . $num;
}
elseif ($num < 0) {
return "-" . $num;
}
else {
return $num;
}
}
function thumbnail_url($value) {
if ($value == "") {
return "no_favicon.png";
}
else {
return $value;
}
}
?>
I use both functions defined in functions.php in index.php. However, only the second function seems to be defined. Whenever trying to use the first function, it spits out an function undefined error.
I've rearranged the code several times and don't know what is wrong with it. The function is defined before I even use it.
Any help is appreciated.
Try changing the name of the functions.php file to something more unique / less generic, like rating.fcns.php.
I would start by replacing the includes with the actual code for the time being, just to see if the issue is with your includes or your functions.
Related
I have created this emptyFields function in my functions.php file and I am using require_once to require functions.php in my validation.php file.
function emptyFields($n, $e, $p, $cp) {
$r;
if ( empty($n) || empty($e) || empty($p) || empty($cp) ) {
$r = true;
} else {
$r = false;
}
return $r;
}
Now, I am using this if clause in the validation.php file to check for any empty fields.
if (emptyFields($name, $email, $pwd, $cpwd) !== false) {
header("location: signup.php?errorid=emptyfields");
exit();
}
Note: Before forwarding input to the emptyFields function, I also use the inputSanitize function which is as follows.
function inputSanitize($n) {
$n = addslashes($n);
$n = htmlspecialchars($n);
$n = trim($n);
}
Apart from the above if clause, I also have other if clauses for email validation, password match etc.
THE PROBLEM: No matter what, It always executes this emptyFields function even when the input is properly provided. The next if clause in line, checks email format using another function, but it never gets executed. From my signup.php file, I input data and I get this query-string so I am assuming that only the emptyFields function gets executed.
location: signup.php?errorid=emptyfields
I have tried using multiple elseif but nothing changes. I have checked the form inputs' names to see whether there is any error in taking input from form but everything seems fine there too. I am using XAMPP.
Any help on this will be greatly appreciated. Thank you.
try below instead of you own, remove the "else" condition which create the conditional tree and slow down when you execute alot of code.
function emptyFields($n, $e, $p, $cp) {
$r = false;
if ( empty($n) || empty($e) || empty($p) || empty($cp) ){
$r = true;
}
return $r;
}
I have two file if.php and index.php, I want to use if.php to store the if condition form if statement. But it seems not working. Is it possible to do it? thank you
if.php
<?php
if(Three == 3) {
//do something
}
?>
index.php
<?php
require_once 'if.php';
else{
//do something
}?>
Update:
Beacase I have a lot of index.php(such as index1.php ,index2.php,index3.php.........................index731.php)
If I need to keep update for if statement.....
First day needto add if(One == 1) , Second day need to add if(One == 1, Two== Three)
so If I need to add value in if statement, I need to change a lot of page!!!!!!
but finally ,I find solution.
if.php
<?php
if(Three == 3) {
$session_admin =true;
}
?>
index.php
<?php
require_once 'if.php';
if($session_admin ==true){
//do something
}else{
//do something
}?>
Each PHP file is compiled individually and it must be syntactically correct. It is not possible to start a control structure, function or class in a file and close it in a different file.
It is, however, possible to return a value from an included file. You can use it to implement the behaviour you want like this:
File if.php:
<?php
if (Three == 3) {
// do something
return true;
} else {
return false;
}
File index.php:
<?php
if (require_once 'if.php') {
// do something (or nothing)
} else {
// do something else
}
However, I do not recommend this approach. The best approach is to encapsulate the test in a function:
File if.php:
function testSomething() {
if (Three == 3) {
// do something
return true;
} else {
return false;
}
}
File index.php:
require_once 'if.php';
if (testSomething()) {
// do something
} else {
// do something else
}
Its wrong way to use if else statement , blew code may help you:
index.php
<?php
if($value == 1) {
include_once('first_file.php');
}else{
include_once('second_file.php');
}
// ** OR **
switch($value){
case '1': include_once('first_file.php');break;
case '2': include_once('second_file.php');break;
}
?>
first_file.php
// put here the code that you want run when $value == 1
second_file.php
// put here the code that you want run when $value == 2
I have a PHP script that executes another PHP script(that contains quite a lot of functions some being recursive) using the include method , multiple times, but the second time i get a error saying that one of the function in the "included" PHP script can not be redeclared.
Now i get that what include does is just inserting the commands from the referenced script in the script that called it and thus I have like the same functions declare too many times resulting in a abomination.
So, could somebody tell me how i should approach this problem?
This is the included script
<?php
$_SESSION['det']=0;
$x=array();
function valid($k)
{
for($i=1;$i<$k;$i++)
if($GLOBALS['x'][$i]==$GLOBALS['x'][$k])
return 0;
return 1;
}
function semn($k)
{
$nr=0;
for($i=1;$i<$k;$i++)
for($j=$i+1;$j<=$k;$j++)
if($GLOBALS['x'][$i]>$GLOBALS['x'][$j])
$nr++;
if($nr%2==0)
return 1;
else
return -1;
}
function determinant($k)
{
$prod=1;
for($i=0;$i<$k;$i++)
$prod*=$_SESSION['matrix'][$i][$GLOBALS['x'][$i+1]-1];
$_SESSION['det']+=semn($k)*$prod;
}
function solve($k,$n)
{
for($i=1;$i<=$n;$i++)
{
$GLOBALS['x'][$k]=$i;
if(valid($k))
if($k==$n)
{
determinant($k);
}
else
solve($k+1,$n);
}
}
$n=$_SESSION['size'];
solve(1,$n);
unset($x);
?>
And this is the script thats includes.
<?php
include 'determinant.php';
if(!$_SESSION['det'])
{
echo "The inverse cant be calculated cause the determinant is equla to 0.";
}
else
{
$detA=$_SESSION['det'];
//Transpusa
for($i=0;$i<$_SESSION['size']-1;$i++)
for($j=$i+1;$j<$_SESSION['size'];$j++)
{
$aux=$_SESSION['matrix'][$i][$j];
$_SESSION['matrix'][$i][$j]=$_SESSION['matrix'][$j][$i];
$_SESSION['matrix'][$j][$i]=$aux;
}
$Dcar=array();
//Matricile caracteristice
for($i=0;$i<$_SESSION['size'];$i++)
{
for($j=0;$j<$_SESSION['size'];$j++)
{
$r=0;
$c=0;
$semn=1;
$a=array();
$_SESSION['matrix'][$i][$j]
for($m=0;$m<$_SESSION['size'];$m++)
{
if($m==$i)
continue;
else
{
for($n=0;$n<$_SESSION['size'];$n++)
{
if($n==$j)
continue;
else
{
$a[$r][$c]=$_SESSION['matrix'][$m][$n];
$c++;
}
}
$r++;
$c=0;
}
}
//Apelarea functiei determinant pentru fiecare matrice
$aux=$_SESSION['matrix'];
$_SESSION['matrix']=$a;
$_SESSION['size']-=1;
include 'determinant.php';
$_SESSION['matrix']=$aux;
$_SESSION['size']+=1;
$Dcar[$i][$j]=($semn*$_SESSION['det'])/$detA;
$semn*=-1;
}
}
for($i=0;$i<$_SESSION['size'];$i++)
{
for($j=0;$j<$_SESSION['size'];$j++)
echo $Dcar[$i][$j]." ";
echo "<br>";
}
}
?>
Use include_once() function.
Follow documentation:
This is a behavior similar to the include statement, with the only
difference being that if the code from a file has already been
included, it will not be included again. As the name suggests, it will
be included just once.
You can use require_once() instead. It's all in the name in this case ;-)
I have a function that takes an input variable and outputs a template with the following call:
outputhtml($blue_widget);
outputhtml($red_widget);
outputhtml($green_widget);
And a simplified version of the function:
function outputhtml($type)
{
static $current;
if (isset($current))
{
$current++;
}
else
{
$current = 0;
}
//some logic here to determine template to output
return $widget_template;
}
Now here is my problem. If I call the function in a script three times or more, I want the output to be one way, but if I only call the function twice, then I have some html changes that need to be reflected in the templates that are returned.
So how can I modify this function to determine if there are only two calls for it. I can't go back after the fact and ask "hey function did you only run twice???"
Having trouble getting my head around how I tell a function that it is not going to be used after the second time and the necessary html modifications can be used. How would I go about accomplishing this?
function outputhtml($type)
{
static $current = 0;
$current++;
//some logic here to determine template to output
if ($current === 2) {
// called twice
}
if ($current > 2) {
// called more than twice
}
return $widget_template;
}
That would not be practical using a static $current inside the function; I would suggest using an object to maintain the state instead, like so:
class Something
{
private $current = 0;
function outputhtml($type)
{
// ... whatever
++$this->current;
return $template;
}
function didRunTwice()
{
return $this->current == 2;
}
}
The didRunTwice() method is asking "did you run twice?".
$s = new Something;
$tpl = $s->outputhtml(1);
// some other code here
$tpl2 = $s->outputhtml(2);
// some other code here
if ($s->didRunTwice()) {
// do stuff with $tpl and $tpl2
}
The only way you can find out if a function was only called twice is by putting the test at the end of your code; but perhaps by then the templates are no longer accessible? Can't tell much without seeing more code.
so im making homepage with three languages.
I am using switch method, here is code -
public function languages()
{
if (isset($_GET['lang']) && $_GET['lang'] != '')
{
$_SESSION['lang'] = $_GET['lang'];
}
else
{
$_SESSION['lang'] = 'en_EN';
}
switch($_SESSION['lang'])
{
case 'en_EN': require_once('language/lang.eng.php');break;
case 'lv_LV': require_once('language/lang.lv.php');break;
case 'ru_RU': require_once('language/lang.ru.php');break;
default: require_once('language/lang.eng.php');
}
}
public function translate($txt)
{
global $text;
return $text[$txt];
}
and it should display in index.php file like this -
<?php $index->translate('search'); ?>
but the problem is that it shows no errors, no notices, no warnings and no translated or default text.
I included function languages() , maybe you can help me with this problem?
EDIT:
im calling $language at start of index.php file - <?php require_once('class.index.php'); $index = new index; $index->languages(); ?> and $text is defined in lang.eng.php; lang.lv.php and lang.ru.php file.
Since you're using a class, I think it's better to use properties instead of globals, it will be easier in future mantainance. Create a class variable holding $text and use that
class Index {
var $text;
public function languages()
{
require(".....");
$this->text = $text;
}
public function translate($txt)
{
if(isset($this->text[$txt]))
{
return $this->text[$txt];
}
else
{
return "no translation";
}
}
}
$index = new Index;
$index->languages();
echo $index->translate('search');
type
<?php echo $index->translate('search'); ?>
Check first whether the session is initialized or not and also place the languages() function call before the translate so that it loads the language prior to translation and also put error_reporting(E_ALL) at top so that any error suppresion will be be removed and also put echo the returned result of translate statement