tmrhmdv
tmrhmdv

Reputation: 172

PHP: include(...); in a variable

I'm trying to include my AdSense code inside a variable like this:

$ad = include_once('../adsense/ad.php');
echo $ad;

In ad.php I'm doing this:

<?php
  return '<script>...</script>';
?>

It's showing my AsSense ad but only once. After that, it's outputting 1. Am I missing something here?

Upvotes: 1

Views: 2097

Answers (3)

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

I think you may be better off using a function for this. You can get the same result, in a much more 'conventional' manner.

Using return in an include will work with include instead of include_once, as mentionned in other answers, but keep in mind include used in this manner makes for non-conventional code structures, harder to re-use, won`t show up in function list definition, among other inconveniances.

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270609

You called include_once(), so the file will only be included one time. After that, it will return TRUE because the call succeeded, but no new content was included. Use plain include() instead if you need to use it multiple times.

$ad = include('../adsense/ad.php');

Upvotes: 4

Frank Farmer
Frank Farmer

Reputation: 39356

use include instead of include_once. That, or only call include_once once ;)

Upvotes: 3

Related Questions