I would like to know the difference between these two symbols - > y = > in php
I would like to know the difference between these two symbols - > y = > in php
I never saw the operator - > in php If you mean "- >" is the one used to refer to a method or object within an object. In other languages, the "." For example:
Class MiClase{
public $x = 0; //Variable pública de la clase "Miclase
public miMetodo(){
//... Contenido del método
}
}
$objeto = new MiClase();
$objeto->x = 5; //Se usa -> para "usar" la variable del objeto
$objeto->miMetodo(); //Lo mismo para usar el método
The Operator = > it is used as an assignment of values for an array. For example:
$miArray = array(
'clave' => 'valor'
);
In php, the characters "- >" they refer to an object.
The typical example:
$coche->endender()
$coche->apagar()
Characters "= >" are the operator equal to or greater than, (which are also used elsewhere as in the associative vectors):
$coche = array(
'marca' => 'SEAT',
'color' => 'blanco',
'modelo' => 'Ibiza'
);
The =>
operator is used as an access mechanism for arrays . This means that what is on the left side of it will have a corresponding value of what is on the right side of it in the context of the array . This can be used to set values of any acceptable type in a corresponding index of a array . The index can be associative (chain-based) or numeric.
<?php
$miArray = array(
0 => 'Grande',
1 => 'Mediano',
2 => 'Pequeño'
);
?>
The ->
operator is used in the scope of the object to access methods and properties of an object. Its meaning is to say that what is to the right of the operator is a member of the object instantiated in the variable on the left side of the operator. Instanced is the key term here.
<?php
$obj = new MiObjeto(); // Crear una nueva instancia de MiObjeto en $obj
$obj->thisProperty = 'Fred'; // Establezca una propiedad en el objeto $obj denominado thisProperty
$obj->getProperty(); // Llame a un método del objeto $obj denominado getProperty
?>
Source: SO
(A) ->
or T_OBJECT_OPERATOR
In PHP, the ->
symbol is an object operator.
Used to access a member of an object. For example:
<?php
//Una clase (Objeto)
class A
{
function foo()
{
echo 'función foo';
}
}
$a = new A();
$a->foo();
?>
As a curious fact ->
is not mentioned between PHP operators a>.
In some cases, to access the members of an object use ::
instead of ->
, in others, you can use any of the indistinctly. For more details on this you can see this SO question in English .
When PHP finds errors relating to ->
it calls it in the following way: T_OBJECT_OPERATOR
.
For example, in a code like this:
<?php
$sTexto="-> se llama T_OBJECT_OPERATOR";
->; //error
echo $Texto;
?>
We will be entitled to the following error:
PHP Parse error: syntax error, unexpected '- >' ( T_OBJECT_OPERATOR ), expecting end of file in ...
(B) =>
or T_DOUBLE_ARROW
The symbol =>
is used in the matrix syntax (arrays) to separate the keys (to the left of =>
of the values, which are on the right.
The PHP manual indicates its use in the array syntax (matrices):
... Syntax "index = > values", separated by commas, defines index and values.
When it shows errors relative to it, PHP calls it T_DOUBLE_ARROW
.
In fact, the following code:
<?php
$sTexto="=> se llama T_DOUBLE_ARROW";
=>; //error
echo $Texto;
?>
I would throw an error message like this:
PHP Parse error: syntax error, unexpected '= >' ( T_DOUBLE_ARROW ), expecting end of file in ...
(C) Where do they appear in the PHP Documentation?
In the section called List of lexical components of the analyzer in the PHP Manual we will find a list of the lexical components of language, among which are of course ->
and =>
. The% -->
symbol does not exist in PHP.