Steve Kim
Steve Kim

Reputation: 5611

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!

Upvotes: 0

Views: 77

Answers (4)

TheSaurus
TheSaurus

Reputation: 357

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.

Upvotes: 1

Janis Vepris
Janis Vepris

Reputation: 577

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

Upvotes: 1

Kiran Dash
Kiran Dash

Reputation: 4956

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

Upvotes: 1

Kapil Agrawal
Kapil Agrawal

Reputation: 72

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

Upvotes: 2

Related Questions