Robert Jones
Robert Jones

Reputation: 408

Counting the value when calling a function

I need some help as I am new on this. I am working on a function to add the day value that start from 0 to 6. I want to count it up the value for each day so I can connect to each url for each day, but on my code it will only show the value as 0 for each day.

Here is the code:

<?php

$channels = json_decode(file_get_contents('channels.json'), true);
$day = 0;

function get_shows($channel_id, DateTime $dt, $duration=1440, $skip_finished = true) 
{
    $url = 'http://example.comapi/GS?cid=' . $channel_id . '&offset=+00.00&day=' .$day++;
    echo $url;
    $day++;
}


if(!isset($_GET['id']) || !isset($channels[$_GET['id']])) 
{
    //some random code.....
}
else
{
    // today + 6 following days
   for($day=0; $day<=6; $day++) 
   {
      // display date      
      echo '<h3>', $dt->format('Y-m-d (l)'), '</h3>';

      // display shows
      foreach(get_shows($channel_id, $dt, $duration) as $show) 
      {

      }
      $dt->add(new DateInterval('P1D'));
      $dt->setTime(0, 0, 0);
    }
}
?>

Output:

2018-05-04 (Friday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-05 (Saturday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-06 (Sunday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-07 (Monday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-08 (Tuesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-09 (Wednesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-10 (Thursday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

Here is what I want to achieve:

2018-05-04 (Friday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=0

2018-05-05 (Saturday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=1

2018-05-06 (Sunday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=2

2018-05-07 (Monday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=3

2018-05-08 (Tuesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=4

2018-05-09 (Wednesday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=5

2018-05-10 (Thursday)
http://example.com/api/GS?cid=44630&offset=+00.00&day=6

Can you please show me an example how I can add up the value for each day that start from 0 to 6 when I am calling on a function??

Upvotes: 2

Views: 111

Answers (4)

Toine H
Toine H

Reputation: 362

The answer is: scope!

You are trying to access a variable that exists outside the function, which does not work in PHP. Each time you call $day++, $day does not exist, so PHP will try to initialize it with the value 0. That's why every url has day=0 in it.

using the "global" keyword is not recommended, because it will eventually lead to very messy code, which can be hard to debug.

I'd recommend something like this (simplified version):

function getShowUrl($channel_id, $day) 
{
    return 'http://example.comapi/GS?cid=' . $channel_id . '&offset=+00.00&day=' .$day;
}

for($day = 0; $day < 7; $day++) {
    echo getShowUrl($channel_id,$day);
}

Using echo inside a function is usually not recommended. Using return enables you to reuse the function for another implementation.

Upvotes: 0

iainn
iainn

Reputation: 17417

Pass $day into the function as a parameter. You're already looping over it but not using it. Your other option is to declare the variable as global, or static within the function body, both of which are not sensible choices.

Change

function get_shows($channel_id, DateTime $dt, $duration=1440, $skip_finished = true)

to

function get_shows($channel_id, DateTime $dt, $day, $duration=1440, $skip_finished = true);

and call it using

get_shows($channel_id, $dt, $day, $duration)

You don't need to increment the variable within the function as well as the loop - let the function do one thing, based on the parameters passed to it.

Upvotes: 0

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

Either use the global that exist by adding global $day in your function:

function get_shows($channel_id, DateTime $dt, $duration=1440, $skip_finished = true) 
{
    global $day;
    $url = 'http://example.comapi/GS?cid='.$channel_id.'&offset=+00.00&day='.$day;
    echo $url;
}

Also remove the increments to $day as they are not needed, your already did this in your loop. Or add an extra parameter and pass $day into your function and use that:

get_shows($channel_id, DateTime $dt, $day, $duration=1440, $skip_finished = true)

Upvotes: 0

vogomatix
vogomatix

Reputation: 5041

Inside a function, variables only have local scope unless you explicitly say otherwise.

$day = 0;

function get_shows($channel_id, DateTime $dt, $duration=1440, $skip_finished = true) 
{
    global $day;
    $url = 'http://example.comapi/GS?cid=' . $channel_id . '&offset=+00.00&day=' .$day++;
     echo $url;
     // don't need this either as you've already post incremented $day
     // $day++;
 }

As others have stated, using global variables is not a good technique and you should really be passing $day into the method.

Upvotes: 1

Related Questions