Jump to content

Recommended Posts

Hi guys,

 

I was wondering if someone could explain how, when I have a function that calculates some stuff relating to google map data and then prints it out, I can get the function to pass this data back as a string rather than print it out. What I'm intending to do is have the function do the calculations and then pass the data back so that I can run a regular job to generate the data (i.e. cache it) and then just read this in each time the page is called rather than run the calculations each time.

 

So, what I have is a functions_map.php file as follows ...

 

<?php

define('DATA_FILE','google_maps_data.inc');
define('LOCK_FILE','google_maps_lock.lck');

# Include generic database definition

require_once("functions_db.php");

# Include function definitions

require_once("functions_calc.php");
require_once("functions_retrieve.php");

function get_google_map_data()
{
?>
        <!-- GOOGLE_MAPS_DATA:Start -->

        <script type="text/javascript">
        <!--

.... code to generate map data

        -->
        </script>

        <!-- GOOGLE_MAPS_DATA:End -->
<?php
}
?>

 

I was then intending to have another page (i.e. the one run regularly to generate the cache) that did the following ...

 

<?php

# Include function definitions

require_once("functions_maps.php");

clearstatcache();

$data = get_google_map_data();

if (( preg_match ("/GOOGLE_MAPS_DATA:Start/i", $data) ) AND ( preg_match ("/GOOGLE_MAPS_DATA:End/i", $data) ))
{
        $fpw = fopen(DATA_FILE, "w");
        fwrite($fpw,$data);
        fclose($fpw);
}

?>

 

And then another "main" page along the lines of ....

 

if ( file_exists(DATA_FILE) )
{
     read in DATA_FILE

     if (( DATA_FILE contains ("GOOGLE_MAPS_DATA:Start") ) AND ( DATA_FILE contains ("GOOGLE_MAPS_DATA:End") ))
     {
          Include contents of DATA_FILE
     } else {
          Run and then include contents of get_google_map_data()
     }
}

 

The problem is that the function get_google_map_data() prints the output to the screen rather than allowing me to return it as the result of the function. Is the only way to do it going to be to build the output line-by-line (i.e. append it to the end of a variable each time) and then return the variable as the result of the function. Or can I write it to a buffer and just return the buffer as the result of the function ??

 

Any help would be appreciated ...

 

Thanks,

Darren

Link to comment
https://forums.phpfreaks.com/topic/63692-functions-and-strings/
Share on other sites

Thanks ... I think !!

 

So, just to confirm ....

 

a) The only way to get it back from a function is to ensure the function has a return statement.

b) The only way to get the value returned is to construct it by creating each line manually ??

 

I guess what I was hoping for was that there was a way to open a buffer ... print whatever I wanted to that buffer ... then return that buffer as the result of the function (thus returning the big amount of text I had created in the function). Is this possible ??

 

For example, there is a LOT of data being returned, it is Javascript and of the format ...

 

                        var point = new GLatLng(21.333333333333, -157.93333333333);
                        var marker = createMarker(point, 46,  'Honolulu', 'USA', 'usa.gif', '62_2004-04-18:61_2004-04-17:52_2004-04-10:', 'honolulu_north_spot.jpg:honolulu_pearl_harbour_arizona_anchor.jpg:honolulu_pearl_harbour_sub_inside_daz.jpg:honolulu_pearl_harbour_sub_scenic.jpg:');
                        map.addOverlay(marker);

                        var point = new GLatLng(-26.25, 28.15);
                        var marker = createMarker(point, 61,  'Johannesburg', 'South Africa', 'south_africa.gif', '89_2006-05-07:87_2006-05-05:', 'joburg_iabl_pres_night_bus_dilan.jpg:joburg_iabl_pres_night_mervyn_tanja_sabrina_sean.jpg:joburg_iabl_pres_night_table_singapore1.jpg:joburg_room_party_daz_drunk.jpg:');
                        map.addOverlay(marker);

                        var point = new GLatLng(7.3, 80.64);
                        var marker = createMarker(point, 66,  'Kandy', 'Sri Lanka', 'sri_lanka.gif', '95_2006-10-21:', 'kandy_botannical_gardens_big_tree_daz_under.jpg:kandy_pinnewala_river_baby1.jpg:kandy_pinnewala_river_bathing_daz3.jpg:kandy_pinnewala_river_small4.jpg:');
                        map.addOverlay(marker);

Link to comment
https://forums.phpfreaks.com/topic/63692-functions-and-strings/#findComment-317404
Share on other sites

You could use heredoc:

<?php
$string <<<EOF
lots of stuff here
bla bla bla
bla bla bla
asdasda
sdasd
EOF;
?>

 

or you could use output buffering:

<?php
ob_start();
?>
lots of stuff here
bla bla bla
bla bla bla
asdasda
sdasd
<?php
$string = ob_get_contents();
ob_end_clean();
?>

Link to comment
https://forums.phpfreaks.com/topic/63692-functions-and-strings/#findComment-317416
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.