Getting Syntax Error While Inputting Shortcode [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm trying to use a shortcode in a php file using the do_shortcode function but I keep getting a syntax error in the shortcode part.
This is the actual error:
PHP Syntax Check: Parse error: syntax error, unexpected 'success'
(T_STRING), expecting ',' or ')' in your code on line 1
I tried to edit the code myself and tried to fix it using several syntax help websites.
<?php echo do_shortcode("[epcl_box type="success"]test[/epcl_box]"); ?>
I am not sure why I get the error message.

Wrong use of quotes:
<?php echo do_shortcode("[epcl_box type="success"]test[/epcl_box]"); ?>
^ ^ ^ ^
Escape the inner quotes:
<?php echo do_shortcode("[epcl_box type=\"success\"]test[/epcl_box]"); ?>
^^ ^^
... or use single quotes:
<?php echo do_shortcode('[epcl_box type="success"]test[/epcl_box]'); ?>
^ ^

You are using Double quotes in type="success" you have to use single quote instead as it is breaking your string:
<?php echo do_shortcode("[epcl_box type='success']test[/epcl_box]"); ?>

Related

Wordpress: Shortcode Error in PHP: syntax error, unexpected '1' (T_LNUMBER) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
Hi I was told to put the following code into my footer PHP
<?php echo do_shortcode("[wpgmza id="1"]"); ?>
My acutal code:
<div class="col-lg-8"><?php echo do_shortcode("[wpgmza id="1"]"); ?> </div>
Im getting the following Error:
Your PHP code changes were rolled back due to an error on line 142 of file wp-content/themes/XXXX/footer.php. Please fix and try saving again.
syntax error, unexpected '1' (T_LNUMBER)
Why is this happening when most forums i read say to that following code?
You are not escaping the quotes in the string and so the string itself ends just before the 1.
What your code is seeing
<?php echo do_shortcode("string"1"string"); ?>
To fix you can change it to single quotes.
<?php echo do_shortcode('[wpgmza id="1"]'); ?>
Here is more info on escaping strings https://www.php.net/manual/en/language.types.string.php

how use strings with $ (not var) in PHP [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I place to the php line a large piece of html code with javascript inserts (with jQuery) in which $ symbols is present.
<?php
$site = <<<SITE_CODE
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;
echo $site;
?>
PHP takes this as a variable and produces an error.
Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
in D:\site\index.php on line 328
Tell me how to get rid of this problem?
If you single quote the EOL string it will not evaluate any variable:
$site =
<<<'SITE_CODE'
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;

Why can't I access array element in string here? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have an array
$array = ['first'=>'hi','second'=>'bye'];
Why following syntax is not working
echo " i wanna print $array['first']";
The error message is
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING) in
But when I tried
echo "i wanna print $array[first]";
did work fine.
So can someone explain what difference single quotes (') making here. And what does above error really mean, any ideas?
here php will not able to parse the multi dimension array from double quote string, to acheive same functionality you have to enquote the array variable inside {} brackets.
Try below line of code it will work without error.
echo " i wanna print {$array['first']}";
I hope this help you in understanding.
Use:
echo "i wanna print ".$array['first'];
Instead of
echo " i wanna print $array['first']";

PHP echo and print Parse Error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
MY code is quite simple.
<?php
echo "hello world";
?>
and i get this message
Parse error: syntax error, unexpected 'world' (T_STRING) in /usr/local/zend/apache2/htdocs/index2.php on line 2
I just copied the code from above on this page because it has the straight bar quotes instead of the curly quotes. I pasted this back into text editor... and wala! it work! thanks guys for the help!!! whoever said the idea about the quotes, i am one upping you or however this site works.

Parse error in PHP file when echoing javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this error mean in PHP?
I keep on getting a "Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in..."
Here's the line thats causing the problem, not sure whats going on here?
$text = "<script>window.setInterval(function(){$('#liveData').load('liveUpdate.php');}, 1000);</script>";
Thanks in advance
You should escape $ in your $text
$text = "<script>window.setInterval(function(){\$('#liveData').load('liveUpdate.php');}, 1000);</script>";
Or you just use single quote.
Because $ in double quote will be followed by a php variable name, $(XXXX is an invalid PHP variable.
So the best way is to separate javascript (jquery) into a .js file.
Try this,
$text = '<script>window.setInterval(function(){$("#liveData").load("liveUpdate.php");}, 1000);</script>';
$text = '<script>window.setInterval(function(){$("#liveData").load("liveUpdate.php");}, 1000);</script>';

Categories