4 votes

difference between if (condition): else: endif and if (condition) {} else {} in php

I have a doubt about php, sopy fairly new to web programming and I still mess a little with php.

my question is, in a php file with the following structure

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>testero</title>
</head>
<body>
<?php
    if(false):
?>
<p>hola</p>
<?php
    else:
?>
<p>adios</p>
<?php
    endif;
?>
</body>
</html>

the html that is displayed corresponds with the php code that surrounds him, that is to say that it shows only goodbye, but however in the following example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>testero</title>
</head>
<body>
<?php
    if(false){
?>
<p>hola</p>
<?php
    }else{
?>
<p>adios</p>
<?php
    }
?>
</body>
</html>

show both options, this happens? someone I could explain why I've been looking on stackoverflow ( I have not found any question relating to this topic) and by the documentation officer of php and can't find anything about it...

Many, many thanks to all and sorry if this is a repeated question even I'm a little bit lost.

1voto

JackNavaRow Points 4207

There is no difference in PHP , they are known as alternative syntax , they are normally used when mixing PHP code with HTML, I personally prefer to work with if(false){}else{} by code convention

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X