Hướng dẫn php concatenate two variables

There are several ways to concatenate two strings together.

Use the concatenation operator . (and .=)

In PHP . is the concatenation operator which returns the concatenation of its right and left arguments

$data1 = "the color is";
$data2 = "red";
$result = $data1 . ' ' . $data2;

If you want to append a string to another string you would use the .= operator:

$data1 = "the color is ";
$data1 .= "red"

Complex (curly) syntax / double quotes strings

In PHP variables contained in double quoted strings are interpolated (i.e. their values are "swapped out" for the variable). This means you can place the variables in place of the strings and just put a space in between them. The curly braces make it clear where the variables are.

$result = "{$data1} {$data2}";

Note: this will also work without the braces in your case:

$result = "$data1 $data2";

You can also concatenate array values inside a string :

$arr1 = ['val' => 'This is a'];
$arr2 = ['val' => 'test'];
$variable = "{$arr1['val']} {$arr2['val']}";

Use sprintf() or printf()

sprintf() allows us to format strings using powerful formatting options. It is overkill for such simple concatenation but it handy when you have a complex string and/or want to do some formatting of the data as well.

$result = sprintf("%s %s", $data1, $data2);

printf() does the same thing but will immediately display the output.

printf("%s %s", $data1, $data2);
// same as
$result = sprintf("%s %s", $data1, $data2);
echo $result;

Heredoc

Heredocs can also be used to combine variables into a string.

$result= <<<EOT
$data1 $data2
EOT;

Use a , with echo()

This only works when echoing out content and not assigning to a variable. But you can use a comma to separate a list of expressions for PHP to echo out and use a string with one blank space as one of those expressions:

echo $data1, ' ', $data2;

I'm using PHP to create a link depending on a variable. Currently I have this line of code which I have used multiple times and works perfectly.

Nội dung chính

  • Recommended Answers
  • All 6 Replies
  • How can I pass two parameters in URL in PHP?
  • How do I pass multiple variables in URL?
  • How do you pass a href variable in HTML?
  • How can I get multiple parameters with same name from URL in PHP?

echo "<a href='specificAssignmentPage.php?
assignName=$value2'>$value2</a>";

The above is all on one line. On the receiving page I have

$assignmentName = $_GET['assignName'];

which retrieves the value from the URL.

However I want to pass multiple variables through the link which I've attempted with

echo "<a href='specificAssignmentPage.php?assignName=$value2'teacherYes=$isTeacher>$value2</a>";

And received with

$assignmentName = $_GET['assignName'];
$isTeacher = $_GET['teacherYes'];

I get this error: " Notice: Undefined index: teacherYes in /Applications/XAMPP/xamppfiles/htdocs/Quests/specificAssignmentPage.php on line 13"

I know that it's probably a basic syntax error with separating my statement but I would really appreciate some help. Thanks!

I'm passing a variable to another page in a url using sessions like this but it seems that I can't concatenate another variable to the same url and retrieve it in the next page successfully

Page 1

session_start();
$event_id = $_SESSION['event_id'];
echo $event_id;

$url = "http://localhost/main.php?email=" . $email_address . $event_id;     

Page 2

if (isset($_GET['event_id'])) {
$event_id = $_GET['event_id'];}
echo $event_id;

echo $event_id shows an error Undefined variable on page 2 but if I use just the event_id in the $url like here

 $url = "http://localhost/main.php?event_id=" . $event_id;

That works fine, but I need to be able to use both variables in the url so that page 2 can retrieve get them.

5 Years Ago

I passed two parameters in the URL. Now I need to retrieve the parameters log_id and dogID onto the next page to process a form.
But I can retrieve the first parameter but i cannot retrieve the second parameter.

Im using

    $log_id= $_SESSION["userid"]; 
    $dogID = $_SESSION["dogID"]; 

Is there another way to get this?

Edited 5 Years Ago by happygeek because: moved

Hi,

can you show the previous step, i.e. the code used to set the session variables?

Jump to Post

If you append parameters to the landing page:

dogReview.php?u=USER&dogID=123

Then you can access those values by using $_GET:

$user  = $_GET['u'];
$dogID = $_GET['dogID'];

$_SESSION, instead, is reserved to values set in the session handler.

See:

Jump to Post

All 6 Replies

cereal 1,524 Nearly a Senior Poster Featured Poster

5 Years Ago

Hi,

can you show the previous step, i.e. the code used to set the session variables?

5 Years Ago

Hello there

The previous step was this to get to the other page

echo("<br /><a class='controls1' href='dogReview.php?u=$username&&dogID=$dogID'><h3>Add Your Review</h3></a>");

I tried using this as well but nothing seems to work..

$dogID = $_GET["dogID"];

cereal 1,524 Nearly a Senior Poster Featured Poster

5 Years Ago

5 Years Ago

that still doesnt seem to work
I keep getting the error:

Column 'dog_reviewed' cannot be null.

My code to insert into the db is :
if($stmt = mysqli_prepare($mysqli,"INSERT INTO DOG_REVIEW(dog_reviewed,id,disciplineComments, challengesFaced,comments,Temperament,healthConcerns)VALUES(?, ?, ?, ?, ?, ?, ?)"))

Where im getting the values from the url for the dog (dog_reviewed) thats being reviewed, and the user(id) that is revewing.

Am I missing something? Or doing this completly wrong?

Im new to php and learning as I go along.

cereal 1,524 Nearly a Senior Poster Featured Poster

5 Years Ago

Am I missing something? Or doing this completly wrong?

It's difficult to say because I don't see the current code you are using. On top of dogReview.php page, right after session_start() set var_dump() to show $_GET and $_SESSION contents:

echo "<pre>";
var_dump($_GET, $_SESSION);
echo "</pre>";

You should be able to see what is sent through the GET request (appending the parameters to the url) and the contents of the current session. If it does not help show the code of this page.

5 Years Ago

How about when you get to your page dogReview.php
the u=$username and the dogID the should show the current value of the up in the brower url

Reply to this topic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

How can I pass two parameters in URL in PHP?

$vars = array('email' => $email_address, 'event_id' => $event_id); $querystring = http_build_query($vars); $url = "http://localhost/main.php?" . $querystring; Additionally, if $event_id is in your session, you don't actually need to pass it around in order to access it from different pages.

How do I pass multiple variables in URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do you pass a href variable in HTML?

href”, append the variable to it (Here we have used a variable named “XYZ”). Then we need to append the value to the URL. Now our URL is ready with the variable and its value appended to it. In the example below, we will append a variable named 'XYZ' and its value is 55.

How can I get multiple parameters with same name from URL in PHP?

Hello readers, Below example will help you to "Get Multiple Parameters with same name from a URL in PHP"..

<? php..

$query = explode('&', $_SERVER['QUERY_STRING']);.

$params = array();.

foreach( $query as $param ).

list($name, $value) = explode('=', $param, 2);.

$params[urldecode($name)][] = urldecode($value);.