1

I am trying to join several variables to create a file path. The file path changes each day and have set it up accordingly. The problem is that after I join them together they they delete them self and leave only the date. Here is the code

$filepath2 = "/data/";
$date = date(Ymd);
$unit = $_GET;
$part1 = strval($unit);
$part2 = strval($date);
$part3 = ".txt";
$filepath = $filepath2 + $part1 + $part2 + $part3;
echo $filepath;

The echo just comes back with 20120713 (the date). Where have I gone wrong?

4
  • Shouldnt you do date("Ymd") instead of w/o quotes? Commented Jul 13, 2012 at 4:21
  • it works this that way? if that is the better way to phrase it then i will (Im quite new to php haha) Commented Jul 13, 2012 at 4:24
  • 1
    From PHP docs: string date ( string $format [, int $timestamp = time() ] ). Meaning, the first parameter needs to be a string. Link: php.net/manual/en/function.date.php Commented Jul 13, 2012 at 4:29
  • 1
    This is how you would CAT in javascript ( for future searchers) Commented Jul 13, 2012 at 4:47

1 Answer 1

4

To concatenate strings in PHP you are not supposed to use + but the concatenation operator; the dot.

Correction of the relevant line in your snippet:

$filepath = $filepath2 . $part1 . $part2 . $part3;

You can read more about string operators by following the link below:

Sign up to request clarification or add additional context in comments.

2 Comments

@Nicholas please remember to flag the post as accepted when that option is available to mark the question as solved.
@refp I will accept the answer but it dosn't let you straight after the question is posted, you have to wait 15 min. I will in 5 min :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.