Explain syntax of <?=# [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I'm following a PHP/AJAX tutorial on form validation. (I'm new to php!).
Could someone explain the syntax of this line:
<?=#$_REQUEST['username']?>
The context is the value attribute of an input field.
I know how $_REQUEST works. I just don't get the <?=# part. I understand <? can be used in lieu of <?php (but isn't always supported!) and <?=$variable?> is special syntax for echoing variables. What does the # symbol do?
Thanks.
Links:
Form validation tutorial
Explanation for special syntax

<?= ?> is the short echo syntax. <?=$var?> is equivalent to <?php echo $var; ?>.
From the PHP manual:
echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
# is the error suppression operator. When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
In short, <?=#$_REQUEST['username']?> will try to print out the value of $_REQUEST['username'] (without ouputting any errors). It's not a good practice and shouldn't be used in your code. If you don't want to display the errors, turn off display_errors in your php.ini configuration and log them instead.

Related

Using <?=$foo?> in PHP [duplicate]

This question already has answers here:
What does '<?=' mean in PHP?
(8 answers)
Closed 7 years ago.
I was upgrading a client's site the other day and noticed that the previous developer had used <?=$foo?> to echo out variable $foo in the code. I know from my days in VBScript that using <%=foo%> works to write out variables to the screen but I had never seen it in PHP nor can I find any documentation on it in Google (part of this is probably because I don't know what this shorthand is technically called).
Is this ok to use or is this deprecated? Does anyone have any further information on this method of echo'ing variables in PHP?
Thanks!
From the manual
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 inline. Otherwise, you
can print it with PHP, for example: '; ?>. Also, if disabled, you must use the long form of
the PHP open tag ().
Note: This directive also affected the shorthand <?= before PHP 5.4.0,
which is identical to <? echo. Use of this shortcut required
short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
asp_tags
Enables the use of ASP-like <% %> tags in addition to the usual <?php ?> tags. This includes the variable-value printing shorthand of <%= $value %>.

What does # do? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I found this tutorial on making a try it editor for a website.
I understood everything in the code except the # in the result.php:
Result.php
<?php
$myCode = #$_REQUEST["code"];
print $myCode ;
?>
Also I tried removing it from the code and nothing changed, so what does it do?
# is a PHP error control operator
PHP supports one error control operator: the at sign (#). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored
Reference
http://php.net/manual/en/language.operators.errorcontrol.php
Example
<?php
/* Intentional file error */
$my_file = #file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = #$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
It ignores/suppresses error messages - see Error Control Operators in the PHP manual.
PHP supports one error control operator: the at sign (#). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an #.
so any error in $myCode = #$_REQUEST["code"]; will be ignored.

What is <?=$varname?> syntax in PHP code? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Previous I bumped into codes like this:
<?php
$var="hello";
?>
<?=$var?>
It simply prints out the content of $var, so... is the syntax equivalent to echo $var?
I'll also appreciate an answer pointing to a related manual page. Since the syntax is not searchable.
Yes, <?=$var?> is the same as <?php echo $var; ?>
From PHP.net manual: echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
You can read more here.

When to use #, and different ways to declare variables? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I'm learning object oriented php.
I've come across some code I don't fully understand.
This code is within a class.
1) code that uses #.
For example:
$this->image = #imagecreatefromgif($filename);
Can some one explain the uses of #.
2)
Also it looks like the script is declaring variables in a way i'm not used to (the $var way).
For example:
$this->ext = $size['mime'];
$ext is not declared before this code is used, but it used after it. Does this create an $ext variable within the object?
Thanks guys!!
The # will suppress errors so that no errors will be shown for that expression.
http://php.net/manual/en/language.operators.errorcontrol.php
# is php's error suppression operator you should never use it
You should handle error instead ignoring and advantage would be you will get long notice which is helpful to debug too
And worst case would be below as described in manual
Currently the "#" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "#" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
# Means "Suppress warning when calling this function".

##variable meaning in php? [duplicate]

This question already has answers here:
What is the use of the # symbol in PHP?
(11 answers)
Closed 9 years ago.
What is the meaning of ##variable in php ?
$fix=##parts;
# is used to suppress any errors that may occur on this line of the code
http://php.net/manual/en/language.operators.errorcontrol.php
When prepended to an expression in PHP, any error messages that might
be generated by that expression will be ignored.
I would not recommend using it at all because this may result in failure of noticing some important error/warning and make debugging a havoc. On a production environment use error_reporting setting that will prevent any errors or warning from showing up while on a development server I would recommend to turn on any error reporting
PHP supports one error control operator: the at sign (#). When
prepended to an expression in PHP, any error messages that might be
generated by that expression will be ignored.
Link : PHP.net explanation
The #operator turns off error handling.

Categories