Solved

passing variables from function to a program

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

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

Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

691 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