Log in

View Full Version : Need help in understanding a code



borris83
03-29-2009, 01:21 PM
Hi, I have some source code downloaded and planning to use it in my site. I came across a function and I need a small clarification

Here is the code:

<?php
// function for showing an error message in a table
function errormess($hiba){
?>
<table width=100% align=center bgcolor=#CCCCCC cellspacing=1 cellpadding=3>
<tr><td bgcolor=#CCCCCC><b>The following error(s) found:</b></td></tr>
<tr><td bgcolor=#FFFFFF align=center><?=$hiba?></td></tr></table>

<?php
}

In
<?=$hiba?>, i see that an equal sign is placed before the variable $hiba. What is this? Does is work like
echo $hiba;

Anyway, I tried to place an = sign before a variable name and placed in a php page and it doesn't work. So, can you pls tell me what
<?=$hiba?> is doing?

Nile
03-29-2009, 03:02 PM
It works like echo. Its only for short tags, and you can't do it in the middle of a php code, for example.

Can't:


<?php
$var = "hello";
=$var;
?>


Can:


<?php
$var = "hello";
?>

<?=$var?>

Schmoopy
03-29-2009, 03:44 PM
This way of doing it is not always recommended though, 90% of the time it's great and works just as you'd expect it to but just like short tags in general, <? ?> sometimes won't work for certain servers and to be absolutely sure it will work no matter what the platform you should use <?php echo $var ?>.