How to Combine Two Strings Together in PHP

How to concatenate multiple strings in php?

You can not concatenate date with string with your existing format. You need to convert it (slashes are removed).

Write date('Y_m_d-H-i-s', strtotime($this->date_of_application));

Write your code as below:

$country = mb_substr($this->country, 0, 4);
$username = $this->user_name;
$converted_date = date('Y_m_d-H-i-s', strtotime($this->date_of_application));
$this->member_id = $country . $username . $converted_date;

What is the best way to add two strings together?

You are always going to create a new string whe concatenating two or more strings together. This is not necessarily 'bad', but it can have performance implications in certain scenarios (like thousands/millions of concatenations in a tight loop). I am not a PHP guy, so I can't give you any advice on the semantics of the different ways of concatenating strings, but for a single string concatenation (or just a few), just make it readable. You are not going to see a performance hit from a low number of them.

PHP string concatenation

Just use . for concatenating.
And you missed out the $personCount increment!

while ($personCount < 10) {
$result .= $personCount . ' people';
$personCount++;
}

echo $result;

How to write a program to concatenate two strings character by character. e.g : JOHN + SMITH = JSOMHINTH

This would be one way to do it (some explanation in the comments):

<?php

$str = 'test';
$str2 = 'test2';

$arr = str_split($str);
$arr2 = str_split($str2);

// Find the longest string
$max = max(array(strlen($str), strlen($str2)));

$result = '';

for($i = 0; $i < $max; $i++){
// Check if array key exists. If so, add it to result
if (array_key_exists($i, $arr)){
$result .= $arr[$i];
}

if (array_key_exists($i, $arr2)){
$result .= $arr2[$i];
}
}

echo $result; //tteesstt2

?>

How to join 2 strings together with delimiter?

if you want to use implode

implode("-", array($first, $last));

or just concat them ?

$first . "-" . $last

or use printf/sprintf ?

printf("%s-%s", $first, $last)

Concatenating strings having dot(period) in php

If the variables contains eg. space, they has to be in quotes. Concat operators cause no problem.

echo "<input type='text' value='" . $value . "' name='" . $input_id . "'/>";

Getting A non-numeric value when combining strings PHP

Use "." instead of "+" to concat strings, "+" is only for Arithmetics.

Combine two strings with for loop

You can use string concatenation. Use the dot . to join to strings, 'a'.'b' will give 'ab'. And $a .= 'c' will append 'c' to the $a variable.

// Create the string
$string = 'Prefix ';
for($i=0;$i<4;$i++)
{
// Append the numbers to the string
$string .= $i;
}
// Append the suffix to the string
$string .= ' suffix';
// Display the string
echo $string;

Result is:

Prefix 0123 suffix

Demo at Codepad.


About the end of your question, you can use this logic:

$page = '<beginning_of_html_page_here>';

// Append things to your string with PHP
$page .= 'something'

$page .= '<end_html_page_here>';

About your first code block, this can also be done by using two functions: range() to generate an array of numbers and implode() to join the array's items:

<?php

// Create the string
$string = 'Prefix '.implode('', range(0, 3)).' suffix';
echo $string;


Related Topics



Leave a reply



Submit