How do I use single quotes inside single quotes?

前端 未结 7 2007
梦谈多话
梦谈多话 2020-12-31 14:10

Can anyone explain how to make this code work?

echo \'
Welcome
\'
7条回答
  •  感动是毒
    2020-12-31 14:14

    echo "
    Welcome ".$_SESSION['username']."
    ";

    or

    echo '
    Welcome '.$_SESSION['username'].'
    ';

    Quick Explain :

    • You don't have to reopen the tags inside a echo String (" ... ")
    • What I have done here is to pass the string "Welcome " concatenated to $_SESSION['username'] and "" (what the . operator does)
    • PHP is even smart enough to detect variables inside a PHP string and evaluate them :

      $variablename = "Andrew";

      echo "Hello $variablename, welcome ";

    => Hello Andrew, welcome

    More infos : PHP.net - echo

提交回复
热议问题