How to change filename with variable

Asked

Viewed 95 times

0

This is the file with the variable I want.

# pt/lang.php
<?PHP
$rp_lang = "pt";
?>

This is the file I want to insert but I want the variable to change the ending either to "pt" or "en" or any other.

# inc/saudacao_pt.php
<?PHP
$rp_saudacao = "Olá bom dia";
?>

This is the file that comes out.

# pt/index.php
<?php
include_once '../pt/lang.php';

include_once '../inc/saudacao_".$rp_lang.".php';
?>

Replace ".$rp_lang." by "pt" works perfectly.

  • Is there an error? Does not give the expected result? Give more details. Tried to use parentheses in the name? I don’t know PHP is kind of weird. really you’re using apostrophe and quotes mixed like this?

  • yes I put parentheses wrote folder by writing her name is en I will change the post

  • No error simply does not assume the "saudacao_pt.php" folder and all its contents but replaces ". $rp_lang." by "pt" in "index.php" works perfectly.

2 answers

3

2


The problem is in concatenation. You are not closing single quotes to concatenate the variable and double quotes are unnecessary.

Do so:

include_once '../inc/saudacao_' . $rp_lang . '.php';
  • Thanks @Lucas hadn’t realized that detail.

  • 2

    @Rjpservidor I wrote in my comment, you did not pay attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.