PHP - concatenate or directly insert variables in string

前端 未结 14 2594
無奈伤痛
無奈伤痛 2020-11-22 04:45

I am wondering, What is the proper way for inserting PHP variables into a string?

This way:

echo \"Welcome \".$name.\"!\"
         


        
14条回答
  •  一整个雨季
    2020-11-22 05:35

    Since php4 you can use a string formater:

    $num = 5;
    $word = 'banana';
    $format = 'can you say %d times the word %s';
    echo sprintf($format, $num, $word);
    

    Source: sprintf()

提交回复
热议问题