What Does a . (Dot) Do in PHP

What does a . (dot) do in PHP?

On its own, that does nothing at all (it's not valid syntax). However, if you have something like this:

<?php

$string1 = "Hello ";
$string2 = "world!";
$string = $string1 . $string2;

echo $string;

?>

You will see Hello world!. The . is the string concatenation operator.

Why is the PHP string concatenation operator a dot (.)?

I think it is a good idea to have a different operator, because dot and plus do completely different things.

What does "a string" + "another string"; actually mean, from a non specific language point of view?

Does it mean

  • Add the numerical value of the two strings, or,
  • concatenate the two strings

You would assume it is the second, but a plus sign is used for numerical addition in all cases except Strings. Why?

Also, from a loosely typed point of view (which PHP is), a php script

$myvar = 1;
$myvar2 = 2;

// would we expect a concatenation or addition?
$concat = $myvar + $myvar2;

The dot notation therefore specifies that it is clearly used for concatenation.

It is not that it is confusing, it is that it is not intuitive because all the other languages do it in a different way. And, is this a good reason to follow the trend? Doing things the way they are always done, is not always the right way.

What is the meaning of three dots (...) in PHP?

This is literally called the ... operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:

This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:

function concatenate($transform, ...$strings) {
$string = '';
foreach($strings as $piece) {
$string .= $piece;
}
return($transform($string)); }

echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

(This would print I'D LIKE 6 APPLES)

The parameters list in the function declaration has the ... operator in it, and it basically means " ... and everything else should go into $strings". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array, ready to be used.

What are these dots either side of the variable?

The period is a String Operator. Specifically, the concatenation operator.

In layman's terms, it glues strings together.

As you can see in the stripslashes documentation, it is a function that returns a string. So, the code is concatenating the first string "SELECT ..." with the result of the function, followed by the final string " ORDER ...".

Is it possible to use period (dot) in a PHP function name?

the answer

to call a method in php, use -> not .

(.) using in PHP

for combining two strings, like:

$str1 = "hello ";
$str2 = "world";

$str12 = $str1.$str2;

hello world

(.) using in js

It's called dot-notation, you can access properties on an object by specifying the name of the object, followed by a dot (period) followed by the property name. This is the syntax: objectName.propertyName;

What does the .= operator mean in PHP?

It's the concatenating assignment operator. It works similarly to:

$var = $var . "value";

$x .= differs from $x = $x . in that the former is in-place, but the latter re-assigns $x.

What does the dot-slash do to PHP include calls?

./ is the current directory. It is largely the same as just file.php, but in many cases (this one included) it doesn't check any standard places PHP might look for a file, instead checking only the current directory.

From the PHP documentation (notice the last sentence):

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory.

PHP | What are three dots before a function arguments?

It indicates that there may be a variable number of arguments.

When the function is called with more than 3 arguments, all the arguments after $next will be added to the $guards array.

You can read about it here.



Related Topics



Leave a reply



Submit