AdamSC
AdamSC

Reputation: 360

PHP Function - Unknown Failure

I'm fairly new to coding and just recently started working on integrating functions into my PHP. I am trying to encode and echo an IP address to Google Analytics's. This is what my custom modifier looks like:

pagetracker._setCustomVar(1, "IP", "<?php include function.php; echo remove_numbers_advanced($_SERVER['REMOTE_ADDR']); ?>", 2);

The function file looks like this:

<?
function remove_numbers_advanced($string)
{
    $numbers = array();
    for($counter =0; $counter <= 10; $counter++) {
        $numbers[$counter] = $counter;


    $replacements = array("A","7","B","6","C","4","D","3","E","F");
    $string = str_replace($numbers, $replacements, $string);
    return $string;
}';

echo remove_numbers_advanced($string);
?>

When I isolated the PHP section of my custom variable in an attempt to test it the page throws a 500 error, suggesting to me that there is something wrong with how I have my script set up.

Please bear in mind I am rather new to this so simple terms and examples would help a ton!

Upvotes: 0

Views: 88

Answers (2)

D3 K
D3 K

Reputation: 682

There are few errors in your function. The correct function is:

function remove_numbers_advanced($string)
{
    $numbers = array();
    for($counter =0; $counter <= 10; $counter++) 
        $numbers[$counter] = $counter;


    $replacements = array("A","7","B","6","C","4","D","3","E","F");
    $string = str_replace($numbers, $replacements, $string);
    return $string;
}

1- You added open curly braces next to for loop but did not close it

2- Also there is " '; " at the closing braces of function. It shouldn't be there.

Upvotes: 1

MhdSyrwan
MhdSyrwan

Reputation: 1633

include function must have a string parameter so put '' around file name

pagetracker._setCustomVar(1, "IP", "<?php include 'function.php'; echo remove_numbers_advanced($_SERVER['REMOTE_ADDR']); ?>", 2);

Upvotes: 0

Related Questions