I'm stuck with this, and till now I didn't catch where I'm making mistake. My code in functions page is:
<?PHP
function getDate()
{
$found= 2014;
$curent= date('Y');
echo $found . (($curent!= $found) ? '-' . $curent: '');
}
?>
and on index page, I called that function with next line of code <?php echo getDate(); ?> Error i get is: HTTP ERROR 500
If you turn on error reporting or consult your server's logs (this is a critical part of developing in PHP), you'll see the following:
Fatal error: Cannot redeclare getDate() ... on line 7
That's because getdate is already a built-in PHP function.
Related
I am using the following PHP code to get the upcoming date of a given day.
For example If the day given in "Tuesday" it will output 01-09-2020.
The function is to show upcoming sale dates of smartphones.
$saledate = "Tuesday"
date_default_timezone_set('Asia/Kolkata');
function dayDate($day) {
return new DateTime('next ' . $day);
}
$nextsale = dayDate($saledate);
if(!empty($saledate)) {
echo "Next Flash Sale on $saledate ";
echo $nextsale->format('d-m-Y'); // 01-09-2020
}
The problem is if the $saledate is empty, it causes the entire web page to not load. What could be the issue ?
This is what the errorlog says
[25-Aug-2020 18:47:15 Asia/Kolkata] PHP Fatal error: Uncaught Error: Call to undefined function dayDate() in /home/customer/www/sitename.com/public_html/index.php:1114
Stack trace:
#0 {main}
thrown in /home/customer/www/sitename.com/public_html/index.php on line 1114
Update: Fixed by adding the $nextsale = dayDate($saledate); inside the if block.
I have slapped together a test PHP script. It would output some remote connection's geo ip based data. Nothing fancy, just a quick prototype.
But I am seeing an odd behavior, so I am asking here if somebody had any clues about it.
PHP is version 5.5.12 on Ubuntu 64 bit.
Here's some code from the geoip_test.php calling script:
require_once ('geoip_utils.php');
$server_geoip_record = geoip_record_by_name('php.net');
echo '<pre>PHP.net web server location: ' . print_r($server_geoip_record, 1);
echo '<br />PHP.net web server local time: ' . \df_library\getUserTime($server_geoip_record)->format('Y-m-d H:i:s');
Nothing fancy at all, isn't it?
Now the simple geoip_utils.php code:
<?php
namespace df_library;
require_once('timezone.php');
// Given an IP address, returns the language code (i.e. en)
function getLanguageCodeFromIP($input_ip)
{
};
// Given a geo_ip_record, it returns the local time for the location indicated
// by it. In case of errors, it will return the optionally provided fall back value
function getUserTime($geoip_record, $fall_back_time_zone = 'America/Los_Angeles') {
//Calculate the timezone and local time
try
{
//Create timezone
$timezone = #get_time_zone($geoip_record['country_code'], ($geoip_record['region'] != '') ? $geoip_record['region'] : 0);
if (!isset($timezone)) {
$timezone = $fall_back_time_zone;
}
$user_timezone = new \DateTimeZone($timezone);
//Create local time
$user_localtime = new \DateTime("now", $user_timezone);
}
//Timezone and/or local time detection failed
catch(Exception $e)
{
$user_localtime = new \DateTime("now");
}
return $user_localtime;
}
?>
When I run the calling script I get:
PHP Fatal error: Call to undefined function df_library\getUserTime() in /var/www/apps/res/geoip_test.php on line 5
The funny part is: if I add this debug code:
$x = get_defined_functions();
print_r($x["user"]);
I get this output:
Array
(
[0] => df_library\getlanguagecodefromip
[1] => df_library\gettimezone
[2] => df_library\getutcdatetime
[3] => df_library\getlocalizedtime
[4] => df_library\getutcdatetimeaslocalizeddatetime
[5] => df_library\getlocalizeddatetimeasutcdatetime
[6] => get_time_zone
)
First of all, I don't understand why the function names are converted to lower case.
But most of all, notice how index 0 shows the empty function function getLanguageCodeFromIP($input_ip) being defined, and that function is right above the one that the interpreter complains about as not being defined!
Why does PHP see the other function in that file but not the one I want to use?
Any ideas are welcome!
There is an extra semi-colon ; after the close bracket of function getLanguageCodeFromIP which causes PHP parser somehow unable to recognize the functions after getLanguageCodeFromIP.
As proven in OP's comment, removing the ; solved the problem.
This question already has answers here:
PHP errors are not shown
(2 answers)
Closed 9 years ago.
I'm feeling my way around php for the first time in years. I'm trying to perform a simple select statement. I've confirmed the statement works directly against mysql. My php does not complete. I'd like to know why my real_query isn't working, I'd also like to know how to coax an error message out of this scenario. Here's the code:
function getRoot($nid)
{
echo $nid; //displays 0
try
{
echo "Hello?"; //This displays
//Why doesn't this work?!
if($mysqli->real_query("SELECT * FROM gem, bundles WHERE gem.nid = bundles.baseNid AND bundles.nid = " . $nid))
{
echo "dafuq?"; //does not display
}
else
{
echo "foo"; //doesn't display
echo $mysqli->error; //doesn't display
}
}
catch (Exception $e)
{
echo "Tralalalala"; //doesn't display
}
echo "teeheehee"; //doesn't display
}
Thanks for your help!
There is no $mysqli variable declared in the function, so your code produce a FATAL ERROR - you are calling a method of a non-object. if $mysqli is a global variable, you have to add global $mysqli; in the beginning of the function
$mysqli is not defined in the function, and PHP throws a fatal error, which is not catcheable using standard exceptions.
You need to enable error reporting (display_errors set to On in php.ini, or through ini_set, eg: ini_set('display_errors', '1'); );
This is my code :
PHP:
if(isset($_COOKIE("cookie_roof_angle")) && isset($_COOKIE("cookie_roof_direction")))
{
$roof_angle = intval($_COOKIE("cookie_roof_angle"));
$roof_direction = $_COOKIE("cookie_roof_direction");
$solarsell_page05_rendement = mysql_query("SELECT value FROM solarsell_page05_pvgis WHERE angle = $roof_angle AND azimut = " . $roof_direction. " ");
echo $solarsell_page05_rendement;
}
else
{
echo "no values";
}
I'm getting this error message :
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\Development\phpFunctions.php on line 19
After some searching around on the web & stackoverflow.com, I found out it may be caused by the isset function, could anyone please explain if this is the problem and why?
If this is not the problem, maybe I did something wrong in my code part, but I can't figure out why.
The Cookies where both set when I got the error.
Sincerly,
Harmen Brinkman.
Syntax error:
$_COOKIE["cookie_roof_angle"] instead of $_COOKIE("cookie_roof_angle")
I'm getting a series of:
"Undefined variable: loginError in /Library/WebServer/Documents/clients . . ."
entries in my Apache error_log which I would like to prevent. I have a simple login.php page which, if there's an error logging in sets the $loginError variable as such:
$loginError = '<p class="text-error">Login Error: '. $layouts->getMessage(). ' (' . $layouts->code . ')</p>';
If there's no error logging in it does this:
$loginError = '';
I then output any errors as such:
if ($loginError !== '') { //line 112
echo $loginError; /line 113
}
I'm getting the entries for the line 112 and 113 noted in my comments above. Anyone tell me how I can prevent the entries appearing? I'm using PHP Version 5.3.6.
thanks
Its saying you should check it is set before using:
One way is with isset()
if (isset($loginError) && $loginError !== '') {
echo $loginError;
}
But in your particular case you may as well use !empty()
if (!empty($loginError)) {
echo $loginError;
}
Hard to say without seeing the rest of your code. Trace through your logic to make sure that every possible branch initializes loginError at some point in its execution. Even better, set it to a default value before you go through the logic.