1

I'm pretty new at this forum, and don't know all functions, like how to paste code, but I'll try my best.

I've been using a PHP script for logging, and are now trying to create a log directory for each day.

The command I can't get to work is this one:

if (!file_exists('Logs/'.$todaydate'/')) { mkdir('Logs/'.$todaydate, 0777, true); }

I haven't fully comprehended how to incorporate data and text together.

my first try:

if (!file_exists('Logs/$todaydate/')) { mkdir('Logs/$todaydate', 0777, true); }

Just created a directory called $todaydate

The second try:

if (!file_exists('Logs/'.$todaydate'/')) { mkdir('Logs/'.$todaydate, 0777, true); }

Just created an syntax error..

Anybody got any idea?

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
JoBe
  • 407
  • 2
  • 14
  • You're missing a concatenction operator in `'Logs/'.$todaydate'/'` (i.e. `'Logs/'.$todaydate . '/'` – Rowland Shaw May 16 '17 at 16:30
  • [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Álvaro González May 16 '17 at 16:32

1 Answers1

0

Your first try failed because you can only use variables within strings when using double quotes. The correct way to do is is the following:

if (!file_exists("Logs/$todaydate/")) { mkdir("Logs/$todaydate", 0777, true); }

As for the second, as Rowland Shaw noted in his comment, you missed a concatenation operator.

if (!file_exists('Logs/'.$todaydate.'/')) { mkdir('Logs/'.$todaydate, 0777, true); }
Daniel
  • 1,229
  • 14
  • 24
  • So if I've understood this, when using single quote ' you need the concatenation (the dots) on both sides of the variable, or else it won't "execute" the variable, but if I put them inside a double quote " , you can mix text and variables.... Or am I way of? – JoBe May 16 '17 at 17:51
  • Yes, you're right. Read: [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Daniel May 16 '17 at 17:59
  • 1
    Great, now I just wrote $myFile = "$dir/$ip.access.log"; and it worked great, and I have struggled with understanding that, and now I just understand, thx – JoBe May 16 '17 at 18:11