-5

Example code in question:

<?php 
function foo($x) {
  ?> // <-- PHP end tag in question
  <ul>
     <li>Coffee</li>
     <li>Tea</li>
     <li>Milk</li>
  </ul>
  <?php
  return ;
}
?>

I am trying to decide if this is an acceptable coding standard / coding practice in a PHP logic file, and what are the downsides of this method in comparison to other html print/concaternation schemes.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    @NSNoob No, [example code is off-topic at Code Review](http://codereview.stackexchange.com/help/on-topic). – Mast Jan 28 '16 at 12:56
  • Oh right. Never used it so didn't know. Sorry. +1 – NSNoob Jan 28 '16 at 12:57
  • Although I don't like inline html (and never used inside a function), I think your code is absolutely consistent with php coding standard. BTW, this a [duplicate question](http://stackoverflow.com/questions/5940428/is-inline-html-valid-inside-a-php-function) – fusion3k Jan 28 '16 at 13:25

3 Answers3

0

The logic you provided is not acceptable.

You can do this:

<? 
function foo($x) {
$var = <<<END
  <ul>
     <li>Coffee</li>
     <li>Tea</li>
     <li>Milk</li>
  </ul>
END;
  echo $var;
}

foo();

// OR You Can do this:
function foo2($x){

$var = <<<END
  <ul>
     <li>Coffee</li>
     <li>Tea</li>
     <li>Milk</li>
  </ul>
END;
return $var;
}
echo foo2();
//If the function is only intended for printing just use global variables as you haven't used the $x argument..
?>

*Also note that you can't have spaces after the

<<<END

and before the

END;

Because of this I haven't indent them to the right inside the function.

  • The original works, but the function you provided is a static function that returns nothing but print a string, The logic here is that you can save a string with html and echo it wherever you want. There is no need to do this in a function. – Abdul Rahman Mohsen Jan 28 '16 at 14:00
  • I had edited my answer. If you want, you can tell me what are you willing to use the function for and I'll edit my answer accordingly. – Abdul Rahman Mohsen Jan 28 '16 at 14:04
0

There is documentation on PHP Strings which you will find very useful. It explains the difference between single quotes, double quotes, heredoc, and nowdoc syntaxes. I don't want to invite opinion in to this issue, but I don't think the method you have demonstrated is very commonly used.

HenryTK
  • 1,287
  • 8
  • 11
0

The function does not return a value, so trying to use it in conjunction with string concaternation techneques is not possible.

For example:

function bar() {
   $concat = "Give me " . foo(null) . " please" ;
}

will output

  • Coffee
  • Tea
  • Milk