0

I want to create filename strings with ".xml" extension.

I have $fileName variable, but I don't know how to combine the variable with ".xml" extension.

For example: From the file names book1, book2, catalog1, catalog2, I want the following output: book1.xml, book2.xml, catalog1.xml, catalog2.xml.

Greg
  • 21,235
  • 17
  • 84
  • 107
Misterk
  • 633
  • 3
  • 9
  • 17

2 Answers2

4

If you have the filename stored within the $fileName variable, you can use string concatenation to join the two strings together.

For example:

$fileName = "catalog1";
$extension = "xml";

// You can use the double quotes to build strings like this:
$fileNameWithExtension = "$fileName.$extension";

// Or you can concatenate using the "." operator:
$fileNameWithExtension = $fileName . "." . $extension;

// Of course, there are many ways to skin a cat:
$fileNameWithExtension = implode(".", [$fileName, $extension]);
$fileNameWithExtension = sprintf("%s.%s", $fileName, $extension);
Greg
  • 21,235
  • 17
  • 84
  • 107
1

In PHP you can concatenate strings using the .

So if the file name is in a variable called $filename then do

$filename = 'a_file_name';
$filename_extn = $filename . '.xml';

Or to add the extension to the existing variable you can use the .= concatenator like so

$filename = 'a_file_name';
$filename .= '.xml';
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149