?
Solved

passing variables from function to a program

Posted on 2012-03-22
2
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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