Conditional HTML embedding between blocks of PHP code?

I am new to PHP. I started to study it like 3 weeks ago. I can not find the answer to this question in StackOverflow, Google or Youtube. documentation on PHP it just confuses me. To answer the question, how does PHP code work with HTML code?

<?php if (something) { ?>
    <p>Hello</p>
<?php } ?>

The p element will only be displayed if something has a true value, like this? ... I definitely thought that the PHP engine ignores what happens around external code blocks (for example, <? Php?>;) And only analyzed what was going on inside.

The code below is usually processed by the PHP engine and sent to the browser without affecting any HTML elements (although it is clearly between the two blocks of code).

<?php echo $something; ?>
<p>Hello</p>
<?php echo $something; ?>

I hope I'm not going to flare up to ask this question, because many people seem to understand how this works, like a tenth of a second.

PS I asked this question in a chat early and thought that I understood correctly, but when I went to implementation, my mind still looked like how it works? I think this is some kind of hack.

+5
source share
5 answers

Now easy. Definitely you need a php tutorial for http://www.tizag.com/phpT/

Here is what you do:

<?php 
//Anything inside me php processes
if($something)
{
    echo "<p>something</p>";
}
//About to stop processing in php
?>
<p>Anything outside of the php statement above will just be printed to the dom</p>

Quick note. Good practice is to separate your PHP from your HTML

<?php if ($something) { ?>  <-- where is the other {
<p>Hello</p>
<?php } ?> <-- oh I see it.
+5
source

In your first example, it’s really true that it <p>Hello</p>will be displayed if and only if “something” returns true.

php- ? > , , if (blah) { ..., PHP- .

?

PHP "" , }, , .

, }, , , PHP , , ,

+3

php html . , script, php- html , . :

<? $someVar = "someVar string value"; ?>
<h1>This is a title</h1>
<? if(1 == 1){?>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<? } ?>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: <?=$someVar;?></p> // <?= is a short hand for echo

:

<h1>This is a title</h1>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: someVar string value</p>

, , script , . html, , php, - , html, html.

+1

PHP- HTML, php code

<?php echo "whatever " ?> 

<?php echo "<h1>Here everything will displayed in h1 </h1> "; ?>

( if, switch ..), , , - true, , { }.

, undefined if, , undefined false.

var_dump($variable)

0

PHP

<!DOCTYPE html>
...
<div>
<?php if ( the_thing === true ) : ?>
    <p>The thing is true! \o/</p>
<?php else if ( the_other_thing === true ) : ?>
    <p>The other thing is true! meh</p>
<?php else : ?>
    <p>Nothing is true :-(</p>
<?php endif; ?>
</div>
...
0

All Articles