Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Syntax Question

Posted on 2012-03-17
5
Medium Priority
?
204 Views
Last Modified: 2012-03-18
Hi,

How could I code this?

If the accounts are not equal to

<?php IF $_SESSION['_amember_user']['data']['account_name'] <> test, web, demo
echo "yes"
else echo ""
?>

Open in new window

0
Comment
Question by:Computer Guy
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37733005
Try this.
<?php
switch ($_SESSION['_amember_user']['data']['account_name']) {
case "test":
case "web":
case "demo":
    echo "yes";
    break;
default:
    echo "";
}
?>

Open in new window


Other possiblity would be to create an array containing test, web and demo and test whether the variable is part of this array.
0
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37733071
Sorry, now I've noticed that I've put negation of your condition, the correct code is here:
<?php
switch ($_SESSION['_amember_user']['data']['account_name']) {
case "test":
case "web":
case "demo":
    echo "";
    break;
default:
    echo "yes";
}
?>

Open in new window

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 37734676
probably little easier to write

if (in_array($_SESSION['_amember_user']['data']['account_name'], array('test', 'web', 'demo'))
{...

Refer http://www.php.net/manual/en/function.in-array.php
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37734686
Grab a copy of this little book and give yourself a few weeks to work through the examples.  It will help you get a good foundation in how PHP and MySQL work together - basic syntax, etc.
http://www.sitepoint.com/books/phpmysql4/

In the instant case, this variable is a single data element.  It is a N-level nested array inside the session array.  The bracket notation indicates array elements.   The concatenation of the bracketed elements indicates "sub-arrays."
$_SESSION['_amember_user']['data']['account_name']

So what you're looking at in that variables is the $_SESSION array.  Inside the session array, you have another array, named _amember_user.  Inside the _amember_user array you have another array, named data.  Inside the data array you have a data element named account_name.  This is an unusual data structure to say the least!

More likely you would have something like this...
$_SESSION['_amember_user'] = 'test';
$_SESSION['data'] = 'web';
$_SESSION['account_name'] = 'demo';

If that is the case, this sort of if() construct might make sense (but consider the logic carefully -- do you want the conjunctive or disjunctive association?)...
if ( 
   ( $_SESSION['_amember_user'] != 'test' )
&& ( $_SESSION['data']          != 'web' )
&& ( $_SESSION['account_name']  != 'demo' ) 
   )
   {
      echo "yes";
   }

Open in new window

In my experience, these kinds of compound if() structures are confusing and often the result of awkward data design.  My guess is that you could simplify the data and it would make the programming easier to write, without any compromise in functionality.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37735254
Thanks for the points - it's a really good question, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

877 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