Solved

passing variables from function to a program

Posted on 2012-03-22
2
267 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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now