What does a . (dot) do in PHP? - php

What does the following command do in PHP?
. $string // ($string is something which I declared in the program)

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.

Taken alone, this is a syntax error. The dot . is the concatenation operator that converts its arguments to strings and concatenates them. For example,
<?php
$string = "x";
$s = 42 . $string;
// $s is now "42x"

Your statement would throw back an error.
The "dot" is a string concatenator. That is, it helps you to combine strings together into another string.
Example.
$full = $part1 . $part2;
Concerning getting started: That's a difficult question. PHP.NET will be your functional looking site. Google-ing just about anything on PHP will direct you there. I'd look at getting a localhost setup of PHP/MySQL/Apache. If you're on a Windows machine, you can get a WAMP server setup.
http://www.wampserver.com/en/
This will drastically speed up your development and testing time. Don't try to FTP everything up to a Web server, as this approach will waste away 10-15% of your working time. Work smart - work local.
Find an existing project (Open Source) with a great community and just try to start something. For example, I recently created DogFriendlyOlrando.com based on WordPress. I was curious as to the abilities of WordPress. It was a fun little project and gave me a good understanding of WordPress' capabilities. You'll learn the most from just diving in and doing. Good luck!

Two string operators are available. The first is the concatenation operator ('.') that returns its right and left argument concatenation.
Second is the concatenation operator of assignment ('.='), which adds the right-hand argument to the left-hand argument. For further details, please read Assignment Operators

The output is displayed directly to the browser like as a given below:
. $string // ($string is something which I declared in the program)

Related

General php format

So, I am just familiarizing myself with php and I see that there are few alternative formats when it comes to writing in php.
What I am confused about is the "dots" or their placements as well other stuffs such as "_".
For example,
<?php
if(!empty($my_post))
{
echo $my_post . ' ' . __('my_post','my_site') . ' + ';
}
?>
It might be a really silly question but could someone explain to me what the function of "dots" in between and just the format itself.
Thanks!
TheSaurus has answered it right. Dots(.) in PHP are the concatenation operators like that plus(+) in java. Whenever you want to build a string with some sub strings, you may use it. There are several other plenty of uses of this, depending on the use.Like One explained in above example.
e.g.:
$line="STACK OVERFLOW";
echo "$line<br/>"
// Some Computation
$line.="is good"; // Here used to concatenate
echo $line
This will output
STACK OVERFLOW
STACK OVERFLOW is good
The dot is the concatenation operator ('.'), which returns the concatenation of its right and left arguments.
<?php
$var = "hello";
$world = "world";
echo "$var" . '$world'; //outputs hello$world
echo "$var" . "$world"; //outputs helloworld
echo "$var" . $world; //outputs helloworld
?>
Read More
The dot
As many have answered before, the dot concatenates strings into a single string. But it's not necessary for bot to be strings. You can concatenate an integer with a string just fine.
<?php
$a = 'Number';
$b = 2;
$c = 'Yay!';
echo $a . $b . $c; // Output: Number2Yay!
?>
The double underscore
In your case, the __() function is just an alias for gettext(): documentation: LINK
Usually, though, the double underscore is used for Magic Methods.
You'll find this piece of text in the documentation:
PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
You can read all about them here: Magic Methods
P.S. You'll probably find THIS LINK very useful for future reference. I really recommend looking through this list :)
Dots are string concatenation operators in PHP.
So, if I write
$a="3";
$b="text";
echo $a.$b;
The result will be 3text.
If you want to add some space between those;
echo $a.' '.$b;
The result will be 3 text.
Please note that ' ' means space character in string form.
Also, please check other questions before submitting one.

In PHP, how can you have quotes around a variable like this?

I saw this code snippet somewhere, wordpress related stuff:
$theme_root = get_theme_root();
$files_array = glob("$theme_root/*", GLOB_ONLYDIR);
It's the "$theme_root/*" part I don't understand. Why quotes around variables?
Why wouldn't the code above instead be written like this:
$theme_root = get_theme_root();
$files_array = glob($theme_root . "/*", GLOB_ONLYDIR);
"Because it works" is a dangerously common reason for doing things in PHP... In this case, it's no big deal, but in other places this principle can be very dangerous.
Anyway, optimally, it should be:
$theme_root . '/*'
Note the use of single-quotes - these are faster to parse because PHP doesn't have to look for variable to interpolate or escape sequences to handle.
That said, however, "fixes" like this are basically micro-optimisation and not something you should dedicate time to.
When you use double quotes, PHP tries to interpret variables inside the string
$test = 'aaa';
echo "test:$test"; //returns 'test:aaa';
echo 'test:$test'; //returns 'test:test';
This syntax could be interesting because it is quite clear, but it is slower if you don't use variables inside the string :
echo "aaaaaaaaaaaaaaaaaa"; //slower
echo 'aaaaaaaaaaaaaaaaaa'; //faster

What's the difference between "string" .$var and "string $var" in php?

In preparing MySQL statements in PHP, I came across lines of codes like this:
$somequery = "some query '$var'";
Where $var is a variable containing a string.
What is the difference with the statement using the string concatenating operator .?
$somequery = "some query " . $var;
The result string is actually the same. If there is any difference, when should I use the one over the other?
There is no difference in the output. The performance differences are negligible. Use whichever one looks nicer to you. Some prefer concatenation because syntax highlighters like it.
The value is the same, but just use one which will make your code easy to read or debug by yourself or by others you have given permission to!

PHP: get array element

Why does this code not work?
echo explode("?", $_SERVER["REQUEST_URI"])[0];
It says syntax error, unexpected '['.
Oddly, this works:
$tmp = explode("?", $_SERVER["REQUEST_URI"]);
echo $tmp[0];
But I really want to avoid to create such a $tmp variable here.
How do I fix it?
After the helpful answers, some remaining questions: Is there any good reason of the design of the language to make this not possible? Or did the PHP implementors just not thought about this? Or was it for some reason difficult to make this possible?
Unlike Javascript, PHP can't address an array element after a function. You have to split it up into two statements or use array_slice().
This is only allowed in the development branch of PHP (it's a new feature called "array dereferencing"):
echo explode("?", $_SERVER["REQUEST_URI"])[0];
You can do this
list($noQs) = explode("?", $_SERVER["REQUEST_URI"]);
or use array_slice/temp variable, like stillstanding said. You shouldn't use array_shift, as it expects an argument passed by reference.
It's a (silly) limitation of the current PHP parser that your first example doesn't work. However, supposedly the next major version of PHP will fix this.
Take a look at this previous question.
Hoohaah's suggestion of using strstr() is quite nice. The following will return from the beginning of the string until the first ? (the third argument for strstr() is only available for PHP 5.3.0 onward):
echo strstr($_SERVER["REQUEST_URI"], "?", TRUE);
Or if you want to stick with explode, you can make use if list(). (The 2 indicates that at most 2 elements will be returned by explode).
list($url) = explode("?", $_SERVER["REQUEST_URI"], 2);
echo $url;
Finally, to use the natively available PHP URL parsing;
$info = parse_url($_SERVER["REQUEST_URI"]);
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
EDIT:
echo current(explode("?", $_SERVER["REQUEST_URI"]));
I believe PHP 5.4 comes with this feature, at long last. Sadly, it will be a while before your average web host updates their PHP version. My Ubuntu VPS doesn't even have it in the default repos.

Concatenation Operator

This might be a silly question but it struck me, and here i ask.
<?php
$x="Hi";
$y=" There";
$z = $x.$y;
$a = "$x$y";
echo "$z"."<br />"."$a";
?>
$z uses the traditional concatenation operator provided by php and concatenates, conversely $a doesn't,
My questions:
by not using the concatenation operator, does it effect the performance?
If it doesn't why at all have the concatenation operator.
Why have 2 modes of implementation when one does the work?
Only slightly, since PHP has to parse the entire string looking for variables, while with concatenation, it just slaps the two variables together. So there's a tiny performance hit, but it's not noticeable for most things.
It's a lot easier to concatenate variables like $_SERVER['DOCUMENT_ROOT'] using the concatenation operator (with quotes, you have to surround the variable in brackets or remove the single quotes in the array index; plus it just makes the string look all ugly). Plus, the concatenation operator allows more flexibility for formatting. For example, you can break up a long string literal onto multiple lines and then concatenate the different parts of it:
$blah = "This is a really really long string. I don't even know how " .
"long it is, but it's really long. Like, longer than an eel " .
"or even a boa constrictor. Wow.";
You can also use the concatenation operator to directly include return values from functions in a string literal (you can't include a function call in a double-quoted string), like this:
$blah = "This has a " . fn_call() . " result, which can't go in the quotes.";
I'm not sure I entirely understand what you're asking here, but I can say that PHP borrows a lot of things from Perl, and one of Perl's mantras is "There's more than one way to do it."
a. Yes. PHP has to parse the string for variables.
b. Because of lines like: echo 'Your Ip address is' . get_ip() . '.';
For reasons A and B.
In some cases your write less with:
$someLongVarName ="Hi";
$someLongVarName .=" there";
VS
$someLongVarName ="Hi";
$someLongVarName = "$someLongVarName there";
Addressing your last question:
Every language has multiple was of doing the same thing. Flexibility is important in every language since any given method may be better the another from situation to situation. The only thing that you should worry about in regards to this is to be consistent in your own code.

Categories