Reputation: 2797
For some reason my PHP function call <? r(); ?>
returns a fatal error. Any help?
<?php
//$r is previously assigned
function r() {
echo ( $r );
};
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<? r(); ?>rs/css/master.css">
</head>
Upvotes: 0
Views: 88
Reputation: 54729
Defining a function changes the scope, where $r
won't be inside that function. Try sending in $r
with the function in this manner:
<?php
function r($r) {
echo ( $r );
}
?>
<link type="text/css" rel="stylesheet" href="<?php r($r); ?>rs/css/master.css">
or defining global $r;
at the beginning of the function (not preferred).
Also, you shouldn't use <?
to open PHP. If all the function does is echo the value of $r
, it would make more sense to just do this:
<link type="text/css" rel="stylesheet" href="<?php echo ( $r ); ?>rs/css/master.css">
Upvotes: 4
Reputation: 272487
If you want to refer to a global object from inside an object, you need to explicitly declare it as global
:
function r() {
global $r;
echo ( $r );
}
But in general, referring to global variables in this way is bad style. You should consider passing in prerequisites as function arguments:
function r($r) {
echo ( $r );
}
...
r($r);
Upvotes: 1