PHP Add if statement html variable [duplicate] - php

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I am trying to add if statemenet into $html variable. But it see php as a html tag. how can add this statement. Thanks
$html = '
<?php if (x=y):?>
<div>
Help me )
</div>
<?php endif ?>
';

Try following
$html = '';
if(x==y):
$html .= '<div> Help me )</div>';
endif;
You can do without closing php tags.
Edited
After #MichaƂSkrzypek comment I have edited and fix mistake in if statement

What you are trying to do is impossible. If you would want to echo the var, you would put <?php inside of <?php, which must result in an error. Only add
<div>
Help me )
</div>
to a var.

Related

Put php value in the end of a link [duplicate]

This question already has answers here:
How to echo inside html tags
(6 answers)
Closed 5 years ago.
Script 1.
add_action('asgarosforum_after_post_author', 'my_function_asgaros_cabinet', 10, 1);
function my_function_asgaros_cabinet($author_id) {
$userData = get_userdata($author_id);
if (!empty($userData) && !empty($userData->description)) {
echo $userData->description;
}
}
Script 2.
I want to put this in script 1 and put ''description'' in the end of that link.
How to do that and whats the right way/code? Cause i think my code is wrong.
<iframe src="http://test.com/me.php?sid='echo $userData->description'"></iframe>
Try with adding the php tag:
<iframe src="http://test.com/me.php?sid=<?php echo $userData->description?>"></iframe>
or
You can try to write the all iframe tag with php:
echo '<iframe src="http://test.com/me.php?sid='. $userData->description .'"></iframe>'

PHP loop is not throwing the correct link [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
for my issue i'll try to be as brief as possible
what am trying to do is reference a page with a specific id in the HTML anchor link with PHP as the below
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php echo $linkName?><br>
and it works fine
now what am trying to do is to make the $id part more dynamic by making looping the number from 1 to 10 and also providing 10 links
the code is
</head>
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=<?php echo $i;?'>Link1</a>";
};
?>
</body>
however what i did notice as the below images indicates when i hover on the links i noticed that i refer to a strange link
and when i cliched on it it takes me to the following link with an id that i did not want as below
http://localhost/PHP_Course/secondPage.php?id=%3C?php%20echo%201;?
i tried researching the subject and i tried escaping the quotation but it does not seem to resolve the problem
Any help please ??
<?php and ?> tags indicate to the PHP preprocessor that anything inside them is code and needs to be parsed, everything outside is just text PHP doesn't touch.
Inside the <?php tag, "<?php" string has no special meaning, so is printed. You do not need to open and close tags all the time, try this:
</head>
<body>
<?php
$linkName = "Second Page";
$id = 5;
for ($i = 0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=$i;'>Link1</a>";
};
?>
</body>
You're echoing a string in PHP, and using <?php... inside that string.
Solution:
echo "<a href='secondPage.php?id=" . $i . "'>Link1</a>";
id=$i will also work, because you can include variables directly in double-quoted strings.
You're echoing the PHP code itself as a string. You don't need to put PHP code inside of PHP code. Just concatenate the values you want to echo:
echo 'Link1';
because you already started an echo statement so you don't need to add another PHP starting and ending tags. just check my code below and try it.
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=".$i."'>Link1</a>";
} ;
?>

Write the text that is between h1 tags [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
How can we take in PHP or jQuery that is written between the <h2> </h2> of each page to put the title of my pages so that each page is different.
Example:
<section class="sub_header">
<h2>Contact</h2>
<h5>Contactez-nous !</h5>
</section>
=> <title>Contact</title>
It's possible ?
PHP, jQuery load() function...?
Thanks
My soluce:
I answer because it could be used for some.
I'll add this little code on important pages. using the superglobal 'REQUEST_URI':
<?php
if ($_SERVER['REQUEST_URI'] == '/index_contact.html') {
$data = 'Contact';
}elseif(
$_SERVER['REQUEST_URI'] == '/index_blog.html') {
$data = 'The blog';
}else{
$data = 'Default title.';
}
// Display the name of the page if data exists
if (isset($data)) {echo $data;} ?>
Hoping that it will be useful for some.
You can try with prag_match to extract data between h1 tag.
$data ="Your content goes here";
preg_match('/<h2>(.*?)<\/h2>/s', $data, $matches);
echo $matches[1];
exit;

php pass value contain question mark? [duplicate]

This question already has answers here:
Question mark in the middle of a URL variable
(3 answers)
Closed 8 years ago.
i want to pass value that contain question mark
i have 2 php example example1.php and example2.php
in my example1.php the code like this
<a href="example2.php?title=what is this?" />
in my example2.php the code like this
<php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
but in example2.php the title become like this what is this the question mark disappear
i already try to use str_replace my question mark become like this what is this"?" but still same at example2.php the question mark disappear
please help me how to pass value that contain question mark
thanks
change <a href="example2.php?title=what is this?" /> into
<a href="example2.php?title=what is this%3F" />
then add
<?php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
this will work for you.

PHP function/class that formats/indents my HTML code? [duplicate]

This question already has answers here:
How to properly indent PHP/HTML mixed code? [closed]
(6 answers)
Closed 9 years ago.
Is there a PHP function/class that cleans my HTML code?
For example:
$html = "<ul><li>item1</li><li>item2</li>";
echo htmlClean($html);
/*
Outputs:
<ul>
<li>item1</li>
<li>item2</li>
</ul>
*/
tidy. or one of the third party php libraries
This might help: Indent HTML Code
keep html out of your php for a start, you should only echo out strings not blocks of html.
<ul>
<?php foreach($items as $item): ?>
<li><?php echo $item['one']; ?></li>
<?php endforeach; ?>
</ul>
If needs must you could try clean the code up with " Heredoc string quoting "
echo <<<EOF
<ul>
<li>"$item1"</li>
<li>"$item2"</li>
</ul>
EOF;

Categories