Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have to start every php file with <?php

Tags:

php

I am new to PHP so sorry for naive question but every PHP file I've seen starts with <?php. Is that mandatory? Are there any well-known workarounds for not having it?

Coming from Node it looks a bit awkward to me.

like image 610
AlexStack Avatar asked Feb 03 '26 07:02

AlexStack


1 Answers

Welcome to PHP Alex!

Yes it is mandatory one. Since PHP sometimes when embedded between HTML, the interpreter finds the code when enclosed within such tags. You can read more about it here

BTW, you can use short open tag <? (But discouraged since it is disabled by default and can be used only after modifying the php.ini configuration file.)

You can also use short echo tag <?= ?> when you want to print. This will be like template engine!


FYI, previously PHP had script tags but it has been removed since the release of PHP 7.0.0. It was like:

<script language="php">
...
</script>

And so is the ASP style tags too <% ... %>.

like image 143
Thamilhan Avatar answered Feb 05 '26 23:02

Thamilhan