Solved

passing variables from function to a program

Posted on 2012-03-22
2
270 Views
Last Modified: 2012-06-21
Hi
i have a php script and inside there is a function does calculation
function sum1($date,$date2,$report){
.... inside this function

$d=$date;
 $d1=$date2;

then i have this calling a graph
<p><img src="graph.php?auto=<?php echo $auto?>&d =<?php echo $d ?>&d2=<?php echo $d2?>"/></p>
how i can pass these variables ($date and $date2 to the graph.php
the graph is working when i put real values ex:2012-03-07...but not with general variables
0
Comment
Question by:asaidi
2 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37755522
@asaidi, I have seen a lot of your questions here and so I am going to recommend that you consider taking a course in PHP programming or at least buy this book and give yourself a little while to work through the examples.  It will provide some structured learning and you will get a lot more understanding of programming with a structured approach.
http://www.sitepoint.com/books/phpmysql4/

Variables that are defined inside the scope of a function are undefined outside the function, unless the global scope is declared.  (This is different in OOP, but in procedural PHP the principle holds true).  Please read this man page.
http://us3.php.net/manual/en/language.variables.scope.php

And for better or worse, computer programming is a process that requires great precision, including clerical speed and accuracy.  When you use confusing variable names (like $d1, $d, $d2) and leave the code in a jumble of letters and punctuation, you set yourself up for errors.  Learn to use coding standards that line up the code neatly.  Then you will have visual cues to whether the code can be expected to work correctly.  Use meaningful variable names.  Avoid undefined variables like $d2.  Avoid unused variables like $report.

Here is an example that shows the way variables might be inserted into HTML inside a function.  It may or may not be exactly what you want, but at least it gives you some place to start.
http://www.laprbass.com/RAY_temp_asaidi.php
<?php // RAY_temp_asaidi.php
error_reporting(E_ALL);

// A SAMPLE FUNCTION
function sum1($date,$date2,$report)
{
    $d  = $date;
    $d1 = $date2;
    $d2 = 'Foo'; // UNDEFINED IN THE PROBLEM DEFINITION

    // ANOTHER UNDEFINED VARIABLE IN THE PROBLEM DEFINITION
    $auto = 'AUTO';

    // CREATE AN HTML STRING
    $htm
    = '<p>'
    . '<img src="graph.php?auto='
    . urlencode($auto)
    . '&'
    . 'd='
    . urlencode($d)
    . '&'
    . 'd2='
    . urlencode($d2)
    . '" />'
    . '</p>'
    . PHP_EOL
    ;

    // RETURN THE HTML STRING
    return $htm;
}

// SHOW THE FUNCTION IN ACTION
$x = sum1('D-1', 'D-2', 'R');
echo htmlentities($x);

Open in new window

Best of luck, and buy that book! ~Ray
0
 

Author Comment

by:asaidi
ID: 37779589
Hi Ray
i had the book and i have read it and i had some videos tuto,no problem
but i think not everytimes the books can help,we must ask other experience people and this in all domains not only IT
sometimes we need something suits as that we cannot find in the book..people cannot think what you are thinking..
and i think that is my last question for php EE
THANK YOU FOR YOUR HELP
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Doing something wrong with this PDO Select Statement... 3 18
How to pass JavaScript generated HTML text form values to PHP? 5 38
AJAX pass along a variable 3 46
mysql db 3 69
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question