Solved

Php -- what is this code doing

Posted on 2012-03-13
7
246 Views
Last Modified: 2012-06-21
Hi,
I'm working on fixing a Form issue from some old code I inherited.
Can someone tell me what exactly this code is doing and returning (or should return)?

Thanks
Nacht

function CCGetFromGet($parameter_name, $default_value = "")
{
    global $HTTP_GET_VARS;
    return isset($HTTP_GET_VARS[$parameter_name]) ? CCStrip($HTTP_GET_VARS[$parameter_name]) : $default_value;
}
0
Comment
Question by:nachtmsk
7 Comments
 

Author Comment

by:nachtmsk
ID: 37715197
I'm particularly curious about the  line that starts   'return isset  ... '
Does a FORM need the data that this line would return in order to be Submitted?

Thanks
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 150 total points
ID: 37715303
That function check if a parameter has been past: if it has been past, then the function returns its value otherwise it returns the default value, that is an empty string.

Does a FORM need the data that this line would return in order to be Submitted?

This depends on how the code is built: I would say: no, the form is submitted but submission can fails or return empty values depending on the code...

Cheers
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 37715531
This is a giant red flag
global $HTTP_GET_VARS;

Open in new window

Suggest you refactor this code completely.  See the "deprecated" notice here.  This is probably left over from PHP4.
http://php.net/manual/en/reserved.variables.get.php

In the case of the instant function, it executes one instruction.  Kind of makes me wonder why it is a function at all - why not just use the native PHP "ternary operator" notation without wrapping it in a function?  See example 2 on this page for an explanation of the way this code works.
http://php.net/manual/en/language.operators.comparison.php
0
How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 14

Accepted Solution

by:
Scott Madeira earned 200 total points
ID: 37715586
Looks like the code was written using CodeCharge as the IDE.  These functions are associated with the program.

It looks like it is taking a parameter name and then checking the $_GET[] array to see if that parameter is in the data coming back from the form submit.  If it is not in the form data (user didn't enter that data on the form before submitting the form) then a default value is provided.

The CCStrip() function most likely strips leading and trailing whitespace from the string data.

"Does a FORM need the data that this line would return in order to be Submitted? "

The flow would be:  user completes form, user submits form, form data is sent to server, this PHP code checks what fields were completed and uses that value or assigns a default value.  So no, the form doesn't require the data to be submitted.  However, the script may need some of that data and if it isn't there then it should return an error message to the browser and ask that the form problems be fixed and then resubmitted.

Hope that helps...
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37715602
The PHP function isset returns true or false, so I am having trouble with the structure of this statement.. Within the isset parentheses is a true false statement using T/F shorthand called ternary operators. It says, if the parameter has been set, then return that value. If it isn't set, return the default value. So why is it in an isset statement? It should return true either way.
Have you tried echoing out what is returned?
It seems like this function is choosing a value for one element of the form, either the value the person entered or chose, or the form element is filled with the default value.
Someone correct me if I am wrong, maybe I am about to learn something new.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 37715845
Within the isset parentheses is a true false statement using T/F shorthand called ternary operators.
False, the return on isset is what is used to evaluate the conditional. These 2 stuctures are identical:
return isset($HTTP_GET_VARS[$parameter_name]) ? CCStrip($HTTP_GET_VARS[$parameter_name]) : $default_value;

// Same as
if( isset($HTTP_GET_VARS[$parameter_name]) ) {
   return CCStrip($HTTP_GET_VARS[$parameter_name]);
} else {
   return $default_value;
}

Open in new window


No points please, just trying to clarify.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37716961
Thanks Derokorian.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

825 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