Use of quotes or single quote in laravel

4

I have a question regarding the use of quotes in laravel to explain I put a snippet of code.

public function index(Request $request){
  $query=trim($reques->('serchText'));
  $categorias=DB::table('categoria')
  ->where('nombre','LIKE','%'.$query.'%')
  ->where('condicion','=','1')
  ->orderBy('idcategoria','DESC')
  ->paginate(7);
  return view('almacen.categoria.index',["categorias"=>$categoria,"serchText"=>$query]);
}

in the part where I return the view use simple quote but if I put normal quotes returns the view, I do not know what to use and why, on the other hand in the section where I assign the value to the parameter "categories"

["categorias"=>$categoria,"serchText"=>$query]

is it ok if I write it with those quotes or should it be with simple food, what is in quotes "categories" comes to be an object like "serchText"?

    
asked by jeancarlos733 02.10.2016 в 06:37
source

1 answer

3

The use of quotes is independent of the framework, that is to say that this is a characteristic of the PHP language, the use of quotation marks is to specify a literal type String, when declaring Strings in PHP there are not only those two forms for do it, but there are 4 ways:

  • Simple quotation marks
  • Double quotes
  • Syntax heredoc
  • Syntax nowdoc (since PHP 5.3.0)

Simple Quoting

This is the most basic way to declare strings. Example:

$a = 'Stack Overflow'
$b = 'Car\'s'

Unlike double-quote and heredoc syntax, variables and escape statements for special characters will not be expanded when they are enclosed within a single quote string. That is to say that \n and $a will be shown as literal strings.

Double Quoting

If a string is delimited with double quotes ("), PHP will interpret the following escape sequences as special characters:

The most important feature of the double quotes of a string is the fact that variable names are expanded. That is, "$a" will show the value assigned to $a and not $ a as a literal string, example:

$a = 3;
echo "$a"; //Mostrará 3 y no $a

In conclusion, single quotes display the text as it is defined, and the use of double quotes interprets special characters and also expands the value of the variables to display it.

Going to your question, both forms are fine, if you wanted to have the variable array indexes for example you could use double quotes to define something like:

["categorias_$id"=>$categoria] //Si por ejemplo $id = 1 obtendrías categorias_1

Although the above could also be done with single quotes as follows:

['categorias_'.$id =>$categoria]

However, this could make the code somewhat illegible in case you concatenate more values.

For more information on the other ways to define strings, check the official PHP documentation link

The following English OS question can also be useful link

    
answered by 02.10.2016 / 21:15
source