0

Well most is in the title. I wonder if it's supposed to be that way or i can do the same without an if(1) condition I'm doing this because my website pages are all as php includes.

Thank you all

Answer retained:

Okay basically the way to do it is simply to include('file.php') as it will be considered out of the current <?php ?> environment.

1
  • 3
    put the code in the question. Not in title. And do whatever feels you are comfort and easier to maintain. Its just matter of opionions. My choice is includes. Commented Nov 20, 2012 at 18:39

3 Answers 3

4

Putting

<?php if(1): ?>
    ...
<?php endif; ?>

around your HTML code in a PHP file will have no effect on the result. You will still be able to include the file without it.

You can think of it like the "default mode" for a PHP file is that it contains HTML content. You only need to add <?php ?> tags if you want to add PHP code. If you're just putting HTML code in a PHP file, they're unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It seems logic but at the same time it's like accepting that an include is echoed automatically in some sort of ways... Thanks i just read the doc of include on php.net
@NicolasManzini you can assume that anything you write outside the <?php ... ?> tags is technically echo'd out.
1

The beauty of PHP is that you can move "in" and "out" of PHP very easily. You can do the following without issues:

<?PHP 
if(whatever) {
?>

your HTML

<?php
include('whatever.php');
?>

more HTML

<?PHP
}
?>

Comments

0

To build on Zak's answer:

You can also use PHP to echo out things that aren't PHP... as long as you quote it appropriately.

<?php 

//HTML 
while ($x < 5) {
 echo "<p> this is html that you can wrap with html tags! </p>";
 $x++;
}

//Javascript
echo "<script type='text/javascript'>
          some javascript code
       </script>"
?>

Although, it's less confusing to just end the php tag to keep things separate.

And you can even use php as you want within html or javascript as long as you put the tags, and as long as the file is saved as a .php file (so PHP can be processed on the server).

Ex:

<script type="text/javascript">
    //set a javascript image array to a php value
    var imgArray = [<?php echo implode(',', getImages()) ?>];
</script>

But if you want to do this the other way around (IE, assign a browser-compiled value, such as a javascript value to a php value), you'll need to use AJAX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.