?
Solved

Php -- what is this code doing

Posted on 2012-03-13
7
Medium Priority
?
250 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
[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
7 Comments
 
LVL 1

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 600 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 14

Accepted Solution

by:
Scott Madeira earned 800 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

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.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 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