Solved

PHP Syntax Question

Posted on 2012-03-17
5
198 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 110

Accepted Solution

by:
Ray Paseur earned 500 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 110

Expert Comment

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

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 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