PHP concatenation

血红的双手。 提交于 2019-12-31 07:57:47

问题


I am trying to get into some PHP and I can't seem to figure out the following.

I can create a string by means of PHP with concatenation:

echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

But what if you want to do this with a variable? (I am guessing WP's functions can be called variables.) I am quite lost here. The following does not work. Or at least, my text editor throws an unknown error.

<?php
if ( is_404() ) {
    echo "<script src='" . get_stylesheet_directory_uri(); . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
  url: '" . get_stylesheet_directory_uri(); . "/sm/swf/',
  onready: function() {
    var mySound = soundManager.createSound({
      id: 'aSound',
      url: 'http://www.mysite.com/sound.mp3',
      volume: '25'
    });
    mySound.play();
  },
  ontimeout: function() {

  }
});
</script>"
}
?>

回答1:


It goes wrong here:

rc='" . get_stylesheet_directory_uri(); . "/s

You have a ; in the middle of your sentence. Also at the end of your string you don't have a ;

Correct syntax

<?php
if ( is_404() ) {
    echo "<script src='" . get_stylesheet_directory_uri() . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
  url: '" . get_stylesheet_directory_uri() . "/sm/swf/',
  onready: function() {
    var mySound = soundManager.createSound({
      id: 'aSound',
      url: 'http://www.mysite.com/sound.mp3',
      volume: '25'
    });
    mySound.play();
  },
  ontimeout: function() {

  }
});
</script>";
}
?>



回答2:


You can also change your approach to avoid such problems. Instead of embeding HTML into PHP you can embed PHP into HTML closing and opening PHP tags:

<?php if ( is_404() ) { ?>
<script src="<?php echo get_stylesheet_directory_uri(); ?>/sm/script/soundmanager2-nodebug-jsmin.js"></script>
<script>
    soundManager.setup({
        url: "<?php echo get_stylesheet_directory_uri(); ?>/sm/swf/",
        onready: function() {
            var mySound = soundManager.createSound({
                id: 'aSound',
                url: 'http://www.mysite.com/sound.mp3',
                volume: '25'
            });
            mySound.play();
        },
        ontimeout: function() {

        }
    });
</script>
<?php } ?>



回答3:


Try this

    if ( is_404() ) {
    echo "<script src='" . get_stylesheet_directory_uri(). "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
    <script>
    soundManager.setup({
    url: '" . get_stylesheet_directory_uri(). "/sm/swf/',
    onready: function() {
    var mySound = soundManager.createSound({
    id: 'aSound',
    url: 'http://www.mysite.com/sound.mp3',
    volume: '25'
});
mySound.play();
},
ontimeout: function() {

}
});
</script>";
}

you are adding ; in between the code which is incorrect.




回答4:


Try this:

<?php
if ( is_404() ) {
echo "<script src='" . get_stylesheet_directory_uri() . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
  url: '" . get_stylesheet_directory_uri() . "/sm/swf/',
  onready: function() {
  var mySound = soundManager.createSound({
  id: 'aSound',
  url: 'http://www.mysite.com/sound.mp3',
  volume: '25'
});
mySound.play();
  },
  ontimeout: function() {

}
});
</script>";
}
?>



回答5:


You can save yourself a lot of headache if you simply save the return value of the function:

<?php
if ( is_404() ) {
$temp = get_stylesheet_directory_uri();
    echo "<script src='$temp/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
  url: '$temp/sm/swf/',
  onready: function() {
    var mySound = soundManager.createSound({
      id: 'aSound',
      url: 'http://www.mysite.com/sound.mp3',
      volume: '25'
    });
    mySound.play();
  },
  ontimeout: function() {

  }
});
</script>"
}
?>


来源:https://stackoverflow.com/questions/16152894/php-concatenation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!