PHP code aposthropy problem what does it mean “”
PHP code aposthropy problem what does it mean “”
I cannot understand the section $type = ''
;
The question is:
what does $type = '';
mean?, particularly apostrophes,
This is the function which writes out the types of variables.
$type = ''
$type = '';
<?php
function what_type($variable)
$type = '';
if (is_integer($variable)) $type .= 'integer, '; else
if (is_float($variable)) $type .= 'float, '; else
if (is_string($variable)) $type .= 'string, ';
if (is_numeric($variable))
$type .= "and is_numeric($variable) === true";
echo $type.'<br />';
$a = 7;
$b = 3.25;
$c = 'some code';
$d = '55';
echo '$a the value of 7 is the type of '; what_type($a);
echo '$b the value of 3.25 is the type of '; what_type($b);
echo '$c the value of ' . "'some code'" . ' is the type of ';
what_type($c);
echo '$d the value of ' . "'55'" . ' is the type of '; what_type($d);
?>
php.net/manual/en/language.types.string.php
– AbraCadaver
Aug 30 at 18:01
That line just initialized
$type
to an empty string. That way the code doesn't throw an error for the concatenation later.– aynber
Aug 30 at 18:01
$type
Single quotes (what you call "apostrophes") are used to wrap a string literal.
– David
Aug 30 at 18:02
2 Answers
2
$type = '';
This simply initializes the variable $type
to an empty string. This is not explicitly needed because PHP defaults undeclared variables to empty values. However, if you do not initialize the variable, and then later try to change it:
$type
$type = $type . 'integer, ';
Or use it:
echo $type;
Then you'll get a warning from PHP that you're trying to change a variable that doesn't exist. Thus, setting the variable to an empty blank string is a common way to avoid that warning.
here $type = ''
is a default value for the $type
variable.
In you example
$type = ''
$type
if (is_integer($variable)) $type .= 'integer, '; else
if (is_float($variable)) $type .= 'float, '; else
if (is_string($variable)) $type .= 'string, ';
if (is_numeric($variable))
$type .= "oraz is_numeric($variable) === true";
You set $type
variable only if at least one of four if
will be true.
For example if $variable
is bool type you wont set $type
variable and get undefined variable error that`s why you need to set some default value for variable to be sure that this variable will exist for sure.
$type
if
$variable
$type
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
'' is empty string.
– u_mulder
Aug 30 at 18:01