Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Doug Hantke
Doug Hantke
10,018 Points

I'm getting a '0' for my body input and I'm not sure why.

<?php
$sid = "xxxxxx";
$token = "xxxxxxxxxxxx";
$client = new Client($sid, $token);
    $first_name = $_POST['first_name'];
    $phone_number = $_POST['phone_number'];
$client->messages->create(
    '+' + $phone_number ,
    array(
        'from' => '+1xxxxxxxxxx',
        'body' => 'Thank you, ' + $first_name + ' for choosing xxxxx. Please take a moment and leave us a review! http://www.google.com')
);

That is the php form that is currently running. I am sending a text message using Twilio and I am getting the text just fine. I'm assuming the problem is how I'm dealing with the 'body' part of the text message. If I remove the first name variable it works just fine. I'm not sure what I'm doing wrong though.

The HTML part is very simple at the moment as I'm just trying to get this to work:

<form action= "make_text.php" method= "post" name= "customer_info">
    <label for= "first_name">Customer's First Name:</label><br>
    <input type= "text" name= "first_name" id= "first_name"><br>

    <label for= "last_name">Customer's Last Name:</label><br>
    <input type= "text" name= "last_name" id= "last_name"><br>

    <label for= "phone_number">Customer's Phone Number:</label><br>
    <input type= "text" name= "phone_number" id= "phone_number"><br><br>

    <input type= "submit" value= "Submit"><a href="/sms/make_text.php"></a>
Lewis Combey
Lewis Combey
2,533 Points

have you tried just writing it as a string? - "Thank you, $first_name for choosing xxxxx. Please take a moment and leave us a review! http://www.google.com";

1 Answer

andren
andren
28,558 Points

In PHP you have to use . (dot) to concatenate text. The plus operator is used exclusively for adding numbers together. Using it on text therefore causes PHP to try to convert the text to a number, and if there are no numbers to be found it just produces 0.

So change your body to this:

'Thank you, ' . $first_name . ' for choosing xxxxx. Please take a moment and leave us a review! \n http://www.google.com')
Doug Hantke
Doug Hantke
10,018 Points

I have obviously never used PHP lol... I didn't even consider that the format to concatenate would be different.