473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is there a shortcut i'm missing?

$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?

Mar 26 '07 #1
7 1347
Lo'oris wrote:
$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?
$name = isset($_GET['name']) ? $_GET['name'] : null;

You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 26 '07 #2
Jerry Stuckle wrote:
Lo'oris wrote:
>$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?

$name = isset($_GET['name']) ? $_GET['name'] : null;

You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.
If setting multiple variables from $_GET, you can also try this:

$parameters=array('name'=>"value",'example'=>"valu e1",'another'=>"value2");
foreach ($parameters as $parameter=>$value)
{
$$parameter=$_GET[$parameter]?$_GET['parameter:$value;
}

--
cb
Mar 26 '07 #3
Christoph Burschka wrote:
Jerry Stuckle wrote:
>Lo'oris wrote:
>>$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?
$name = isset($_GET['name']) ? $_GET['name'] : null;

You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.

If setting multiple variables from $_GET, you can also try this:

$parameters=array('name'=>"value",'example'=>"valu e1",'another'=>"value2");
foreach ($parameters as $parameter=>$value)
{
$$parameter=$_GET[$parameter]?$_GET['parameter:$value;
}
Which is only slightly less dangerous than running with register_globals
on. Someone can come in and set any variable in your script by setting
it in the query string. And if you miss initializing a variable you've
got a huge potential security breach.

One reason register_globals is no longer enabled by default.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 26 '07 #4
Jerry Stuckle wrote:
Christoph Burschka wrote:
>Jerry Stuckle wrote:
>>Lo'oris wrote:
$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?

$name = isset($_GET['name']) ? $_GET['name'] : null;

You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.

If setting multiple variables from $_GET, you can also try this:

$parameters=array('name'=>"value",'example'=>"val ue1",'another'=>"value2");

foreach ($parameters as $parameter=>$value)
{
$$parameter=$_GET[$parameter]?$_GET['parameter:$value;
}

Which is only slightly less dangerous than running with register_globals
on. Someone can come in and set any variable in your script by setting
it in the query string. And if you miss initializing a variable you've
got a huge potential security breach.

One reason register_globals is no longer enabled by default.
Not really. It think it's a clever way to do it. Save you some coding time.

If you see, he only allows the variable in the parameter to be changed.

But then again, it's only my oppinion.

Hendri
Mar 27 '07 #5
Hendri Kurniawan wrote:
Jerry Stuckle wrote:
>Christoph Burschka wrote:
>>Jerry Stuckle wrote:
Lo'oris wrote:
$name=$_GET['name'];
if (!$name)
$name="value";
>
i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?
>
$name = isset($_GET['name']) ? $_GET['name'] : null;

You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.
If setting multiple variables from $_GET, you can also try this:

$parameters=array('name'=>"value",'example'=>"va lue1",'another'=>"value2");

foreach ($parameters as $parameter=>$value)
{
$$parameter=$_GET[$parameter]?$_GET['parameter:$value;
}

Which is only slightly less dangerous than running with
register_globals on. Someone can come in and set any variable in your
script by setting it in the query string. And if you miss
initializing a variable you've got a huge potential security breach.

One reason register_globals is no longer enabled by default.

Not really. It think it's a clever way to do it. Save you some coding time.

If you see, he only allows the variable in the parameter to be changed.

But then again, it's only my oppinion.

Hendri
Not at all. I key in

http://www.example.com?admin=1

And now in your program $admin=1. And what if $admin is the variable
which indicates I'm an admin?

This effectively does exactly what register_globals does - just limits
it to the $_GET variables.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 27 '07 #6
Jerry Stuckle wrote:
Hendri Kurniawan wrote:
>Jerry Stuckle wrote:
>>Christoph Burschka wrote:
Jerry Stuckle wrote:
Lo'oris wrote:
>$name=$_GET['name'];
>if (!$name)
> $name="value";
>>
>i can't figure out how to shorten this thing. Is there some kind of
>operator i don't know about?
>>
$name = isset($_GET['name']) ? $_GET['name'] : null;
>
You should always test with isset() to see if a value passed to your
page is set or not. Otherwise you will get a notice if you have them
enabled.
>

If setting multiple variables from $_GET, you can also try this:

$parameters=array('name'=>"value",'example'=>"v alue1",'another'=>"value2");

foreach ($parameters as $parameter=>$value)
{
$$parameter=$_GET[$parameter]?$_GET['parameter:$value;
}
Which is only slightly less dangerous than running with
register_globals on. Someone can come in and set any variable in
your script by setting it in the query string. And if you miss
initializing a variable you've got a huge potential security breach.

One reason register_globals is no longer enabled by default.

Not really. It think it's a clever way to do it. Save you some coding
time.

If you see, he only allows the variable in the parameter to be changed.

But then again, it's only my oppinion.

Hendri

Not at all. I key in

http://www.example.com?admin=1

And now in your program $admin=1. And what if $admin is the variable
which indicates I'm an admin?

This effectively does exactly what register_globals does - just limits
it to the $_GET variables.
Well in order to do that the $parameters variable have to include admin
is it.

Hendri
Mar 27 '07 #7
Lo'oris wrote:
$name=$_GET['name'];
if (!$name)
$name="value";

i can't figure out how to shorten this thing. Is there some kind of
operator i don't know about?
The current PHP method is:

$name = $_GET['name'] ? $_GET['name'] : 'value';

It was proposed that PHP 6 should have a new function "ifsetor" which
would achieve the same thing using this syntax:

$name = ifsetor($_GET['name'], 'value');

However, this proposal was rejected in favour for making the middle part
of the current method optional, so you could write:

$name = $_GET['name'] ? : 'value';

However, if you want to use the above, you'll need to wait for PHP 6, and
your code will not be backwards-compatible with earlier versions of PHP.

Personally, I've written my own ifsetor() function, and just use that.

/**
* Implementation of proposed (but discarded) PHP6 built-in function.
*
* @param mixed $value Preferred value.
* @param mixed $fallback Fallback value.
* @return mixed isset($value) ? $value : $fallback
*/
function ifsetor ($value, $fallback)
{
if (isset($value))
return $value;
return $fallback;
}

I use this most often for loading settings. Assuming I have a bunch of
settings from a file stored in some array:

$mode = ifsetor($settings['mode'], 3); // default mode is 3

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Todd Peterson | last post by:
I'm encountering some wierd behavior with a <link> tag over an HTTPS connection, vs. an HTTP connection... In an ASP/HTML page on my website, I've add a <link rel="shortcut icon"...> in order to...
2
by: Top Gun | last post by:
I am trying to get the output from a setup project to place an entry on the menu plus a shortcut on the option, but setting the property's in User's Desktop and User's Programs Meny to true doesn't...
1
by: Tom | last post by:
I created an msi using the Setup Wizard, but don't seem to be able to create a shortcut to the applicaton on the desktop (or prompt for one) or the menu. Also, I would like to add an "uninstall"...
3
by: Terry Bell | last post by:
I have some code that adds entries to the "Module Compiled" popup menu. Works fine in A97. You can open a module, right click, and the new entries are there for you to click. I converted it to...
1
by: exitstageleft | last post by:
Hi. I am missing the ability to use 'CTRL-SHIFT-F' shortcut key to search files in visual studio. It shows no shortcut key combination on the file menu for Edit->Find & Replace->Find in Files. ...
4
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
1
by: tombsy | last post by:
Hello guys, I am missing the right click/build shortcut menu in my Access 2000 code modules. I cant remember the fix, any help please ? many thanks
2
by: variaas | last post by:
Hey guys, So I created a setup project for my new app, but I don't know how to add a shortcut to the exe on the desktop. For now, I've created a link to the "Primary Output" of the project, but...
4
by: Steve | last post by:
Hi All I have a windows forms application written in VB.net 2005 which places a shortcut in All users desktop during installation, using MS setup and deployment Occasionally when a customer...
10
by: Matthew Wells | last post by:
I'm trying to make a shortcut that will open an mdb in the same folder as the shortcut - regardless of where the file/shortcut may be. In other words, I want to be able to copy and paste the two...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.