HTML inside sprintf - php

Is it possible to do something equivalent to this in PHP >= 5.3.0?
echo sprintf(_('By %s'), ?>
<span class="author-name"><?php echo $authorName; ?></span>
<?php ); ?>
If I use said syntax I receive the following error, which is being caused by the first ?>,
Parse error: syntax error, unexpected '?>' in ...
Basically, what I'm trying to do is to insert HTML with PHP tags inside, as a parameter for the sprintf function without treating the HTML as a string ('<span class="author-name">' . $authorName . '</span>').

Related

display div inside php using echo

I want display my button if it have isset($_GET). I am trying to do like this.
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
its giving me error like
Parse error: syntax error, unexpected 'project_id' (T_STRING), expecting ',' or ';' in C:\xamppp\htdocs\mayank\add_project.php on line 101
I am not getting idea what I should do for echo project_id in div. Let me know if someone can help me for that.
Thanks
Thats incorrect to use echo inside another echo and how can you start a new php tag without closing the first.
The correct way is to concatenate the variable along the string passed in echo, here is how
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
instead of breaking the php tags break the ' quotes to concatenate the value in the string.
Why do you need again tag inside echo just use it as below:
<?php
if(isset($_GET['project_id']))
{
echo ('<div class="add_btn_primary"><a href="manage_project_users.php?project_id='.$_GET["project_id"].'>Project Users</a></div>');
}
?>

Error durring editing functions.php (Wordpress)

I'm getting the error
Parse error: syntax error, unexpected '}' in /var/www/humanityx/wp-content/themes/wordpress-bootstrap-master/functions.php on line 1127
/* Display the post meta box. */
function smashing_post_class_meta_box( $object, $box ) {
?>
<?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>
<p>
<a class="post-btn-blue">Button</a>
<br />
<span class="short-blue">[buttonblue]Link Text[/buttonblue]</span>
<br />
</p>
<?php
}
I run the whole code in PHP Code checker and there is no problem. Also PHPStorm is not viewing any error.
https://pastebin.com/U45wqnpX
I have PHP 7.1
Just my two cents.....
In the function 'smashing_add_post_meta_boxes' you set the callback 'smashing_post_class_meta_box'.
According to the WordPress codex:
$callback
(callable) (Required) Function that fills the box with the desired content. The function should echo its output.
When I look at 'smashing_post_class_meta_box' the function perhaps does not really 'echo' anything and therefore gives unexpected output resulting in the unexpected '}' not picked up by PHPStorm?
Like I said, not sure, could not test, but wanted to help.

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE

I might have a syntax error or something but I don't see nothing.
<select id="cd" name="cd">
<?php
while($row=mysql_fetch_array($cdresult)) {
echo "('<option value='$row['Poblacion']'></option >'.'<br />)";
}
mysql_close($link);
?>
</select>
On the echo line, I have the error :
[error] [client] PHP Parse error: syntax error, unexpected
T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or
T_NUM_STRING in /var/www/slimtest/views/nuevo.php on line 89
Maybe you can help, because I don't see the error D"=
When using array variables inside of strings it's usually better to use the complex syntax:
echo "('<option value='{$row['Poblacion']}'></option >'.'<br />)";
Alternatively you can remove the quotes in the array key:
echo "('<option value='$row[Poblacion]'></option >'.'<br />)";
PHP String Variable Parsing
This should work:
<select id="cd" name="cd">
<?php
while($row=mysql_fetch_array($cdresult)) {
echo "<option value=".$row['Poblacion']."></option><br/>";
}
mysql_close($link);
?>
</select>
Try changing echo line to this:
echo '<option value="' . $row['Poblacion'] . '"></option >';
This line is a mess
echo "('<option value='$row['Poblacion']'></option >'.'<br />)";
First off, you can't use other characters around an <option> tag (the <br> tag is meaningless there). And then you leave the text of the tag blank. Finally, you're using double quotes around the whole thing, leaving PHP to try and interpret it. My bet is you're trying to do this instead.
echo '<option value="' . $row['Poblacion'] . '">' . $row['Poblacion'] . '</option>';
This will generate a proper tag AND populate it with the text of your field as well (so users can see what they're selecting). The way you had it, even if it were proper HTML, you'd have a dropdown of nothing but blank entries.

php quotes in quote in quotes

I have the following code:
echo "<script type='text/javascript'>
iframe0document.getElementById('jform_articletext').value = '<?php echo \"<p align='center'><iframe src='aufgaben/'+cookieValue+'/'+cookieValue+'.html' width='322' height='497' frameborder='0'></iframe></p>\";?>';}</script>";
but it has a problem with the quotes, I tried almost everything but it says always:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /usr/www/users/mathea/joomla/plugins/content/DirectPHP/DirectPHP.php(58) : eval()'d code on line 118
Ok the question should be changed to php nesting in php. I need a way to negate the offect of the php nesting like it's possible for quotes with \"
Updated
You made PHP tags within echo. That's pretty wrong!
As I understand OP:
$text = htmlentities('<?php echo \'<pre><p align="center"><iframe src="aufgaben/"+cookieValue+"/"+cookieValue+".html" width="322" height="497" frameborder="0"></iframe></p></pre>\'; ?>');
echo "<script type='text/javascript'>
iframe0document.getElementById('jform_articletext').value = ".$text.";
</script>";
You need htmlentities function to encode characters to their html equivalents and print out php tags az plain text.
But you are already in "php mode"!
Why you reopen php tag on
'<?php echo
?
Then, what is "iframe0document.getElementById"?

PHP embed html image as a link

How do I go about using an image as a link in php? I have never put two html elements together in one echo so it's kinda new for me.
Here's my code:
htmltest.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?
require("includes/conn.php"); //link to the database
?>
<html>
<title>HTML with PHP</title>
<body>
<?php
echo "<img src="homelogo.jpg" />";
?>
</body>
</html>
That's my code. I get the following error:
PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home6/dreamsm2/public_html/htmltest.php on line 11
Can anyone tell me what I'm doing wrong? Any help would be appreciated.
Change the line to:
echo '<img src="homelogo.jpg" />';
OR
echo "<img src=\"homelogo.jpg\" />";
The problem, as the error somewhat suggests, is that the PHP interpreter can't figure out where your string is supposed to start and end. Using \" escapes the quotes. Using ' around the string gives a unique string delimiter around the string, so you are free to use double quotes inside.
Note, if you needed both single and double:
echo '<img src="homelogo.jpg" />';
You can also use ' instead of " for strings, e.g.
This works: echo '"Hello!"'; => "Hello!"
This wont work: echo "'Hello'";
SIMPLY DO THIS:
echo '<img src="Downloads_clip_image010.jpg" />';
For WordPress
<div class="floatLeft">
<a href="http://trophydevelopers.com">
<img src="<?php bloginfo('template_url'); ?>/images/powered-by.png">
</a>
</div>

Categories