Solved

PHP Syntax Question

Posted on 2012-03-17
5
195 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 108

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 108

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now