When to use #, and different ways to declare variables? [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 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".

Related

difference between $variable and #$variable in php [duplicate]

This question already has answers here:
'At' symbol before variable name in PHP: #$_POST
(5 answers)
Closed 9 years ago.
Can you please tell me the difference between
$variable and #$variable in php
<?php
curl function abc
{
get information of url and return information string
}
$html=abc();
$doc=DOMDocument();
#$doc->LoadHTML($html);
?>
here if we take normal variable it gives error why its so
and whats the difference
A # before a function call means "suppress warnings".
So, #$doc->LoadHTML($html); suppresses warnings from the method call (LoadHTML()).
In general this is a bad idea, because the warnings mean you are doing something wrong, and you would better fix that instead of playing deaf.
The # operator tells the compiler to ignore the error that PHP could give, its advised not to use it.
Suppress warning when accessing that property, if for instance $html was undefined then no error is displayed, see http://davidwalsh.name/suppress-php-errors-warnings
# is called Error Control Operator, it can be prepended before expression to disable error reporting for that expression.
Please see this post for more information: Suppress error with # operator in PHP

Is using # (at) operator a bad idea? [duplicate]

This question already has answers here:
Should I use # in my PHP code?
(6 answers)
Closed 9 years ago.
Is writing:
$name = #$_GET['name'];
instead of:
$name = isset($_GET['name']) ? $_GET['name'] : null;
is some bad coding practice? Can I use # as a normal operator or only in some specific cases?
Yes, using the # operator is considered bad practice.
It is well documented that it has performance implications.
In addition, supressing error messages is a sign of bad practice; you should write your code to expect errror and handle them defensively. Simply swallowing the errors behind an # symbol means that your code will never know that the error occurred, nor what the error actually was; this can often lead to further problems later in your code.
There are some cases where it is unavoidable to use # but this is not one of them, so you should avoid doing it.

When is the '#' operator used in PHP and MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the use of # symbol in php?
PHP functions and #functions
What does the “#” symbol do in SQL?
Often when looking through online resources / questions including code on this website, I often come across the '#' operator.
In the PHP code I see, the # operator generally comes before a method such as #fread(). However, by the looks of the code, it wouldn't have actually made a difference whether or not fread() was called with or without the '#' in front of the method.
In the MySQL code, the # operator generally comes before a column/field name. I believe that the # is used to access a default variable instantiated at some point manually by the user, alike to an env variable in PHP.
Please can you tell me if my assumptions are correct, and if not, when and why the '#' sign or operator should in fact be used?
Thanks,
Max.
In PHP it means suppress any notices/warnings raised by this function eg mail() will throw an error when it fails to send. You can do
$Success = #mail(...)
To suppress the error and handle success/failure yourself. See the Error Control Operator page for more information.
In MySQL, # is used to prefix user-defined variables
In PHP the at sign (#) denotes that any error messages that might be generated by that expression will be ignored.
In MySQL the # before a name denotes a local, user-defined variable.

# and & in PHP, when used with variables [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've seen in PHP some variables that are set like this:
$var = #$something;
Or functions set like this:
$var = &my_function();
What effect do the # and the & have?
You have them backwards. &$variable means "a reference to this variable." #my_function() means "call this function and suppress any errors or warnings that it produces."
# means "don't report errors"
& means "take a reference to the following variable instead of copying its value"
# operator in php is used to ignore errors in that statement.
Manual: http://www.php.net/manual/en/language.operators.errorcontrol.php
& is reference operator.
Manual: http://www.php.net/manual/en/language.references.whatdo.php
# causes to hide all errors. In case of variables, it might be E_NOTICE informing about variable not existing.
The &my_function(); is invalid - & is normally used for references, it doesn't nothing in this case.

What is the meaning of # in php? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is the use of # symbol in php?
I like to know the purpose and meaning of # in php codes.
For instance, #umask or #ini_set. What's the difference without # and with #?
PHP's error suppress operator used to suppress error messages.
SideNote: Avoid it as much as you can, also it slows down performance heavily and not allowed to be used in ini_get and ini_set functions in future php versions.
"Swallow an error", continue despite an error occuring. An non-critical operation augmented with # will not abort script execution.
# symbol is an error control operator check out manual here

Categories