PHP concatenation

2020-05-11 01: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>"
}
?>

5条回答
手持菜刀,她持情操
2楼-- · 2020-05-11 02:00

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 } ?>
查看更多
Lonely孤独者°
3楼-- · 2020-05-11 02:03

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>"
}
?>
查看更多
地球回转人心会变
4楼-- · 2020-05-11 02:05

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.

查看更多
再贱就再见
5楼-- · 2020-05-11 02:12

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>";
}
?>
查看更多
beautiful°
6楼-- · 2020-05-11 02:19

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>";
}
?>
查看更多
登录 后发表回答