deskeay
deskeay

Reputation: 273

How to string two variables together in php

I am new to php. And string concatenation is difficult for me. I want to concatenate the following string:

$branch = 'apple';
$rules = "(product=='iphone')andManufacturer==".$branch;
// result "(product == 'iphone')andManufacturer ==apple"

But the string I want:

"(product=='iphone')andManufacturer=='apple'"

Thanks.

Upvotes: 0

Views: 56

Answers (3)

DevTastic
DevTastic

Reputation: 11

You can check functions like sprintf() and printf()

$rules = sprintf("(product=='iphone')andManufacturer=='%s'", $branch); 

Upvotes: 1

Alan Yu
Alan Yu

Reputation: 1542

Try this(Added single quote to the string):

$branch = 'apple';
$rules = "(product=='iphone')andManufacturer=='".$branch."'";
echo $rules;

Another solution(Wrap the single quotes in double quotes to branch variable):

$branch = "'apple'";
$rules = "(product=='iphone')andManufacturer==".$branch;
echo $rules;

OUTPUT:

(product=='iphone')andManufacturer=='apple'

Upvotes: 1

Zafar Shah Faizi
Zafar Shah Faizi

Reputation: 544

You can directly put your $branch variable in "" quotes, no need for concatenation.

$branch = 'apple';
$rules = "(product=='iphone')andManufacturer=='$branch'";
echo $rules;

OR

$branch = "'apple'";
$rules = "(product=='iphone')andManufacturer==" . $branch;
echo $rules;

OUTPUT:

(product=='iphone')andManufacturer=='apple'

Upvotes: 1

Related Questions