PHP heredoc assignment to a variable error -
i have following code display form variable using heredoc. following error;
parse error: syntax error, unexpected '' (t_encapsed_and_whitespace), expecting identifier (t_string) or variable (t_variable) or number (t_num_string) in c:\xampp\htdocs\php\2000.php on line 38
line 38 is: <form method="post" action="$_server['php_self']">
<?php if ( !$_post ){ $display_block = <<<eob <form method="post" action="$_server['php_self']"> <p> <label for="email"> e-mail address: </label> <br/> <input type="email" id="email" name="email" size="40" maxlength="150"/> </p> <fieldset> <legend> action: </legend> <input type="radio" id="action_sub" name="action" value="sub" checked/> <label for="action_sub"> subscribe </label> <br> <input type="radio" id="action_unsub" name="action" value="unsub"/> <label for="action_unsub"> unsubscribe </label </fieldset> <button type="submit" name="btnsubmit" value="send"> submit </button> </form> eob; //closing delimiter must start no spaces nor tabs infront of it. } echo $display_block; ?>
my questions are;
- in case, heredoc acts $display_block = " string inside double quotes " ? should use { }
action="{$_server['php_self']}
in order wrap array item in double quotes?
but when use that, new error;
parse error: syntax error, unexpected end of file in c:\xampp\htdocs\php\2000.php on line 59
line 59 is: ?>
- why cannot use action attribute?
action = “<?php echo $_server[‘php_self’]; ?>”
because; have spaces before eob;
(closing identifier) being 4 of them; remove them.
as per manual on heredoc:
warning
it important note line closing identifier must contain no other characters, except semicolon (;). means identifier may not indented, , there may not spaces or tabs before or after semicolon. it's important realize first character before closing identifier must newline defined local operating system. \n on unix systems, including mac os x. closing delimiter must followed newline.
if rule broken , closing identifier not "clean", not considered closing identifier, , php continue looking one. if proper closing identifier not found before end of current file, parse error result @ last line.
heredocs can not used initializing class properties. since php 5.3, limitation valid heredocs containing variables.
example #1 invalid example
<?php class foo { public $bar = <<<eot bar eot; } ?>
you didn't close off </label
=> </label>
.
and action="$_server['php_self']"
needs read action="<?php $_server['php_self']; ?>"
equivalent doing action=""
need use here make work, action=""
.
you can use bracing method action="{$_server['php_self']}"
if want/must use syntax.
"why cannot use action attribute?
action = “<?php echo $_server[‘php_self’]; ?>”
"
if actual quotes used being curly quotes “ ”
, need changed regular double quotes "
, contribute parse errors.
however, <?php echo
, ;?>
have needed removed.
footnotes:
i noticed <input type="radio" id="action_sub" name="action" value="sub" checked/>
, tells me using script subscription site.
having radio button set default "yes, subscribe me", illegal in many countries, in mine being canada.
the canadian anti-spam legislation prohibits being default (already selected) , user him/herself needs select it, not them.
- you should made aware of , there heavy fines/consequences type of action.
either uncheck default, or use single checkbox unchecked (have them check it) , using conditional statement against it.
Comments
Post a Comment