Can HTML be embedded inside a PHP "if" statement?

I would like to embed HTML inside the PHP if statement, if possible, because I think the HTML will appear before executing the PHP if statement.

I am trying to access a table in a database. I created a drop-down menu in HTML that lists all the tables in the database, and as soon as I select a table from the drop-down list, I click the submit button.

I use the isset function to see if the submit button has been pressed and run a loop in PHP to display the contents of the table in the database. So, at the moment I have a complete table, but I want to run a few more queries in this table. Hence the reason I'm trying to execute more HTML inside an if statement. Ultimately, I try to either update (1 or more contents in a row or several rows) or delete (1 or more rows) contents in a table. What I'm trying to do is create another drop-down menu that matches the column in the table to simplify the search in the table and the radio buttons that match whether I want to update or delete the contents in the table.

+100
html php
Apr 6 '09 at 17:19
source share
7 answers
<?php if($condition) : ?> <a href="http://yahoo.com">This will only display if $condition is true</a> <?php endif; ?> 

Upon request here elseif and more (you can also find in the docs )

 <?php if($condition) : ?> <a href="http://yahoo.com">This will only display if $condition is true</a> <?php elseif($anotherCondition) : ?> more html <?php else : ?> even more html <?php endif; ?> 

It's simple.

HTML will only be displayed if the condition is met.

+319
Apr 6 '09 at 17:23
source share

Yes,

 <?php if ( $my_name == "someguy" ) { ?> HTML GOES HERE <?php; } ?> 
+35
Apr 6 '09 at 17:22
source share

Yes.

 <? if($my_name == 'someguy') { ?> HTML_GOES_HERE <? } ?> 
+10
Apr 6 '09 at 17:43
source share

Using close / open tags is not a good solution for two reasons: you cannot print PHP variables in plain HTML, and this makes your code very difficult to read (the next block of code starts with an end bracket } , but the reader has no idea has what it was before).

Better use heredoc syntax. This is the same concept as in other languages ​​(e.g. bash).

  <?php if ($condition) { echo <<< END_OF_TEXT <b>lots of html</b> <i>$variable</i> lots of text... many lines possible, with any indentation, until the closing delimiter... END_OF_TEXT; } ?> 

END_OF_TEXT is your separator (it can be basically any text, such as EOF, EOT). Everything between them is considered a string using PHP, as if it were in double quotes, so you can print variables, but you do not need to avoid quotes, so it is very convenient for printing html attributes.

Please note that the closing delimiter must begin at the beginning of the line, and the semicolon must be placed immediately after it without other characters ( END_OF_TEXT; ).

Heredoc with single-quoted string behavior ( ' ) is called nowdoc . No parsing is done inside nowdoc. You use it the same way as heredoc, only you put the opening delimiter in single quotes - echo <<< 'END_OF_TEXT' .

+2
Jul 15 '16 at 8:56
source share

So, if the condition is equal to the required value, then the php document will run "include" and include this document in the current window for example:

`

 <?php $isARequest = true; if ($isARequest){include('request.html');}/*So because $isARequest is true then it will include request.html but if its not a request then it will insert isNotARequest;*/ else if (!$isARequest) {include('isNotARequest.html')} ?> 

`

+1
Sep 04 '17 at 4:54 on
source share
 <?php if ($my_name == 'aboutme') { ?> HTML_GOES_HERE <?php } ?> 
0
Aug 18 '14 at 8:00 a.m.
source share

I know this is an old post, but I really hate that there is only one answer that suggests not mixing html and php. Instead of mixing content, you should use template systems or create a basic template system yourself.

In php

 <?php $var1 = 'Alice'; $var2 = 'apples'; $var3 = 'lunch'; $var4 = 'Bob'; if ($var1 == 'Alice') { $html = file_get_contents('/path/to/file.html'); //get the html template $template_placeholders = array('##variable1##', '##variable2##', '##variable3##', '##variable4##'); // variable placeholders inside the template $template_replace_variables = array($var1, $var2, $var3, $var4); // the variables to pass to the template $html_output = str_replace($template_placeholders, $template_replace_variables, $html); // replace the placeholders with the actual variable values. } echo $html_output; ?> 

In HTML (/path/to/file.html)

 <p>##variable1## ate ##variable2## for ##variable3## with ##variable4##.</p> 

The output of this will be:

 Alice ate apples for lunch with Bob. 
0
Jan 13 '19 at 5:28
source share



All Articles