Viewed   81 times

i'm starting to work with php basics and i have some problem understanding how mix code with strings.

I found a great and useful style to print string blocks but i don't know the name and i'm not able to find examples.

the code below return me the error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /web/htdocs/food/user/index.php on line 120

<?php   
$html_str = <<<STR
    <li><img alt="hello" src="$path_images/pencil.png"/><a title="hello" href="$path_pages/$page/action">Details</a></li>

STR;
print $html_str;
?>

can someone help me to find where i'm wrong and the name of this syntax style?

thanks v

 Answers

2

I've found the problem!
in the example I've posted it can't return the error:

Working code

<?php
$str = <<<STRING
hello! this is a working string<br/>
and i can do too many things with heredoc syntax!
STRING;

print $str;
?>

Not working code

<?php
     $str = <<<STRING
     syntax error!<br/>
     syntax error!<br/>
     why?
     STRING;

     print $str;
?>

The problem are the tabs before the close tag STRING; which are considered part of tag, so the close tag is not interpreted "STRING;" but "        STRING;", that's why it doesn't work.

hope it come usefull for someone else.

Tuesday, August 30, 2022
 
2

Well, the following is invalid syntax:

if ($this->isQuestion$q($query)){

Try this instead:

foreach ($questions as $q) {
    if ($result = $this->{'isQuestion' . $q}()) {
        return $result;
    }
}
return false;
Tuesday, September 20, 2022
 
gunner
 
2

You can't use a foreach in a echo statement! Just break them up like this:

echo '<script>
      $( document ).ready(function() {
      $("#Drafts > tbody:last").append(';

foreach($this->central as $key2 => $value2) {
    echo '<tr> 
            <td><a href='. $value2->Token .'>Edit</a></td>
            <td class=T id='. $value2-    >Token.'>'.htmlentities($value2->Title).'</td>
          </tr>';
}

echo  ');});</script>';
Monday, October 3, 2022
 
raam
 
1

You need to be running PHP 5.4+ to use shorthand arrays

Thursday, October 27, 2022
 
1

Anonymous functions is a feature added in 5.3

For earlier versions, create a named function and refer it by name. Eg.:

function file_check_method_func($n) {
    $n = absint($n);
    if(1 !== $n) { $n = 0; }
    return $n;
}
$valid['file_check_method'] = array_map('file_check_method_func', $input['file_check_method']);

or inside a class:

class Foo {
  protected function file_check_method_func($n) {
    $n = absint($n);
    if(1 !== $n) { $n = 0; }
    return $n;
  }
  function validate($input) {
    $valid = array();
    $valid['file_check_method'] = array_map(array($this, 'file_check_method_func'), $input['file_check_method']);
    return $valid;
  }
}

I would strongly suggest not to rely on create_function.

Sunday, September 4, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :