Solved

passing variables from function to a program

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
when to use sequences in mysql 4 30
javascript: add id amounts 5 47
send post values 7 26
PHP strings vs array errors 13 23
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

832 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