0

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!

Steve Kim
  • 4,396
  • 8
  • 39
  • 82
  • 5
    possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Marc Jun 23 '15 at 06:51
  • 1
    It simply concatenates or combines the values of the declared variables. – Sahan De Silva Jun 23 '15 at 07:19

4 Answers4

2

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
1

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.

danbahrami
  • 974
  • 1
  • 8
  • 14
TheSaurus
  • 337
  • 1
  • 3
  • 20
1

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

Kiran Dash
  • 4,261
  • 6
  • 39
  • 75
1

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 :)

Community
  • 1
  • 1
Janis Vepris
  • 577
  • 3
  • 13