This question already has answers here:
What is the use of the # symbol in PHP?
(11 answers)
Closed 9 years ago.
I have this piece of code which I need to add to my website:
if (isset($_REQUEST['j']) and !empty($_REQUEST['j'])) {
header("Location: http://atmst.net/utr64.php?j=" . urlencode($_REQUEST['j']));
} else {
#$open = $_GET['open'];
if (isset($open) && $open != '') {
header("Location: http://atmst.net/$open ");
exit;
}
It has the following syntax I've never seen before - #$ near the open variable. What does the # char do?
# is the error suppressor.
NEVER USE IT. You ALWAYS want to capture and handle errors. Error suppression makes it harder for you to debug your code.
Code should be:
if (isset($_REQUEST['j']) and !empty($_REQUEST['j'])) {
header("Location: http://atmst.net/utr64.php?j=" . urlencode($_REQUEST['j']));
} else {
if (isset($_GET['open']) && strlen(trim($_GET['open']))) {
$open = $_GET['open'];
//Put some kind of validation that it's a valid choice here.
header("Location: http://atmst.net/$open ");
exit;
}
}
As Jessica mentioned It supresses errors. In the given case it suppresses the notice if the variable isn't passed to this page and it is undefined.
Details: http://php.net/manual/en/language.operators.errorcontrol.php
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
The error is probably something stupid, but I can't find the mistake to save my life.
I have this piece of code in my program:
$validate = (
$num_rows > 1 ||
$num_rows == 0);
if ($validate) {
$data['code'] = 2;
} else {
$data['code'] = 1;
while ($a = $resultados->fetch_assoc()) {
$data['response'] = $a;
}
}
I 'believe' its written correctly, however when ran, I'm getting the error 500 and the logs read:
Syntax error: Unexpected '$num_rows' (T_VARIABLE) on line 21...
Line 21 is the second validation for $num_rows.
Any suggestions?
Thanks in advance!
I think you just forgot to put a semicolon at the end of the code line that comes before your line starts with $validate = ...
Would be awesome to see the lines above this code snippet.
$validate = ($num_rows > 1||$num_rows == 0);
Try this, sometimes the space between || and the second condition make a strange syntax error. I'm curious, which IDE/text editor are you using.
I think it isn't a PHP core problem, but it come from something else, please give us more context :)
By the way, you could replace your condition by
$validate = $num_rows >= 0;
it's exactly the same and you avoid your problem
This question already has an answer here:
Php: Warning: Header may not contain more than a single header, new line detected in
(1 answer)
Closed 6 years ago.
I have the following PHP code which re-directs based on the grade
if ($grade == 'Admin')
{
header("location:Admin\FINAL_adminhome.html");
}
else if ($grade == 'Employee')
{
header("location:Admin\FINAL_adminhome.html");
}
else if ($grade == 'Security Guard')
{
header("location:Security guard\securityhome.html");
}
else if ($grade == 'Receptionist')
{
header("location: Reception\recphome.html"); // Line 53
}
For "Admin", "Employee" and "Security Guard", it redirects correctly. However, I get the following error for "Receptionist":
Warning: Header may not contain more than a single header
\r means carriage return (ASCII code 13). Try escaping the slash like this
header("location: Reception\\recphome.html");
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I may be doing something horribly wrong, so that's why I want to ask this question.
{
$pic = channelart.png;
$pic_loc = $_FILES['pic']['tmp_name'];
mkdir($userRow['user'], 0777, true);
$folder="/.$userRow['user']";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('Successfully updated channel art.');</script><?php
}
else
{
?><script>alert('Failed to update channel banner.');</script><?php
}
}
I got this error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/video10p/public_html/labs/au/index.php on line 7
Create folder before upload image :-
mkdir($userRow['user'], 0777, true);
Your code like this :-
{
$pic = 'channelart.png';
$pic_loc = $_FILES['pic']['tmp_name'];
mkdir(trim($userRow['user']), 0777, true);
$folder="/.trim($userRow['user'])";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('Successfully updated channel art.');</script><?php
}
else
{
?><script>alert('Failed to update channel banner.');</script><?php
}
}
Change this $pic = channelart.png; to this $pic = "channelart.png";
You are getting the error because PHP is expecting a String, Variable or statement. The T_ENCAPSED_AND_WHITESPACE token picked up on this line tells us that the parser came upon another assignment, when it was expecting a String, Variable or Numeric.
PHP Tokens are extremely useful to debugging and I would strongly advise taking a look at the page linked above.
This should solve your posted error above.
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'); );
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.