Solved

PHP Syntax Question

Posted on 2012-03-17
5
196 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 109

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 109

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

773 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