0

I have this code:

<?php echo $form->fileField($model, 'avatar_url', array('style' => 'display:none')) ?>
<a href="#" class="thumbnail" style="width: 96px" id="avatar">
<?php if (empty($model->avatar_url)) { ?>
    <img data-src="holder.js/96x96" id="avatar-img">
<?php } else { ?>
    <img src="<?= Yii::app()->baseUrl . $model->avatar_url ?>" id="avatar-img">
<?php } ?>

When I replace <?php to <? , the code does not work and the browser shows an error.

PHP notice Undefined variable: class

Maybe I missed an extension in PHP?

Clay
  • 4,700
  • 3
  • 33
  • 49
Mr.P
  • 45
  • 7
  • 1
    I don't see `$class` in your code anywhere.. Also I don't see `< ?php`, or `< ?`.. – chris85 Jan 11 '16 at 03:59
  • You need a semicolon (`;`) after your `echo` statement. Also, don't use ``. Just use ` – elixenide Jan 11 '16 at 04:00
  • sorry, i edited my questions, i'm newbie in PHP and i can't find solution from Google :D – Mr.P Jan 11 '16 at 04:00
  • Why are you trying to `replace < ?php to < ?`? Is there a specific instance in your code sample that you're trying to do this with? And what is the outcome that you're expecting? – Guildencrantz Jan 11 '16 at 04:01
  • My problem is why this code when use with , it working in some situation but it not working in my server, when i replace by – Mr.P Jan 11 '16 at 04:02
  • 1
    What PHP versions are you using on each server? Maybe this will help you, http://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags. – chris85 Jan 11 '16 at 04:03
  • @EdCottrell the `;` isn't needed with the `?>` at the end of the line I thought. – chris85 Jan 11 '16 at 04:04
  • 1
    @chris85 unless I'm mistaken, that isn't true with some older versions of PHP. In any case, leaving it off is bad practice, for sure. – elixenide Jan 11 '16 at 04:05
  • Describe `it working in some situation but it not working in my server`. In what scenario does it work? – chris85 Jan 11 '16 at 04:08
  • i deploy with WAMPP in window server, it working, but in linux server it not working :( – Mr.P Jan 11 '16 at 04:09
  • Both servers are running the same PHP version? – chris85 Jan 11 '16 at 04:10
  • tks for your help, i missing configure short_open_tag in php.ini, Now everything ok – Mr.P Jan 11 '16 at 04:12
  • Was the answer that your other server is running PHP < 5.4 and the `=` needed the short tag directive? – chris85 Jan 11 '16 at 04:13
  • No, short_open_tag in my linux server is Off, i must set On , every thing ok – Mr.P Jan 11 '16 at 04:14
  • `=` is valid in PHP 5.5 with the short tag directive off. I think you should get both servers running the same version. `5.4.0 The tag = is always available regardless of the short_open_tag ini setting.` http://php.net/manual/en/language.basic-syntax.phptags.php – chris85 Jan 11 '16 at 04:25

3 Answers3

4

Sounds like you need to enable short_open_tag in your php.ini. Find the line short_open_tag in your php.ini and set the value to 1:

short_open_tag = 1

After you make the change, restart your web server.

Read more here: http://php.net/manual/en/ini.core.php#ini.short-open-tag

short_open_tag

Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

Community
  • 1
  • 1
Clay
  • 4,700
  • 3
  • 33
  • 49
3

In PHP5 <? is not allowed any more. We always should use <?php. By the way you can do this doing some tricks. But <?php is a good practice so don't leave it.

Imran
  • 4,582
  • 2
  • 18
  • 37
2

Although technically <? can be used with PHP it's considered best practice to always use <?php instead.

Jonathan Head
  • 428
  • 3
  • 7