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
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I've been working on a wordpress project for the past few days on my localhost.
When I was done with it, I tried uploading it on my live host.
But when the installation was finished, it showed a syntax error, saying "Parse error: syntax error, unexpected '[' in ..."
The code is the following :
function Consulin_header_style() {
if (Consulin_meta('enable_custom_header_style') == 1):
$images = Consulin_decode(Consulin_meta( 'header_image'));
$header_images = '.page-title { background-image: url('. wp_get_attachment_image_src($images[0],'flat-page-title')[0].');}';
else:
if ( get_header_image() != "" ) {
$header_images = '.page-title { background-image: url('. get_header_image().');}';
} else {
$header_images = '.page-title { background-image: url('.CONSULIN_LINK.'images/page-title.jpg) ; }';
}
endif;
wp_add_inline_style( 'Consulin-style', $header_images );
}
The 4th line of the code which says "$header_images = ..." is the problem.
I have absolute zero knowledge of programming, so I'd appreciate some help here.
P.S: It works perfectly fine on localhost.
As the comment states, this error is related to php versions. You have an older php version in your production environment.
Still, to make your code backwards compatible, you could change your code to something like this:
// …
if (Consulin_meta('enable_custom_header_style') == 1):
$images = Consulin_decode(Consulin_meta( 'header_image'));
$imageData = wp_get_attachment_image_src($images[0],'flat-page-title');
$header_images = '.page-title { background-image: url('. $imageData[0].');}';
else:
// …
Explanation:
In older versions of php you could not use the return value of a function directly. The solution above stores the return value in a temporary variable, and using that when referencing its [0] index.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm fairly new to PHP, and I don't know a lot about it. I have had a go, but I don't know where I have gone wrong.
The code:
<?php
include('config.php');
if(isset($_SESSION['username']) and $_SESSION['moderator'] {
} else {
header("location:noaccess.php");
}
?>
The error: Parse error: syntax error, unexpected '}' in E:\LocalWebHost\htdocs\forum\test.php on line 4
<?php
include('config.php');
if(isset($_SESSION['username']) and $_SESSION['moderator']) {
} else {
header("location:noaccess.php");
}
?>
In line 3, your if statement needs closing ).
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.
I have this code :
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];}
I read about this error on this site but i have no ideea how to apply the fix on my particular code. I`m sorry if i repost the problem.
Try doing: var_dump($userdata->yim); to verify if yim does in fact exist and contains the key 'text'.
Or even just var_dump($userdata);
$userdata->yim['text'] is not set, so you should check for that first:
if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
However, if that section of code relies on that value, something is wrong before that and this will just hide that problem.
In order to debug this you will need to utilize print_r() so that you can see the contents of your object or array:
// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';
// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];
}
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