Jump to content

Array - Need Detailed Explanation


zhafran

Recommended Posts

Hi there,

 

I'm working on Ben Falk's PHP Login Using OOP & MVC and I need detailed explanation about this function :

 

	function setAlert($value, $type = null)
{
	if ($type == '') 
	{
		$type = $this->alertTypes[0];
	}
	$_SESSION[$type][] = $value;
}

 

What really the use of the blank array and how it works really?

 

Hopefully Ben or anybody can explain it deeply because I just a beginner here. Thanks!

Link to comment
Share on other sites

$myArray[] = 'value1';

$myArray[] = 'value2';

 

This blank square bracket -> [] means add a new node into the array.

 

It's essentially same to:

 

$myArray[0] = 'value1';

$myArray[1] = 'value1';

 

it's just that you don't need to keep count, so it's perfect for use inside functions and loops.

Link to comment
Share on other sites

Assuming I remember correctly... (I'll update this post if there are any errors once I look at the full source code)

 

function setAlert($value, $type = null)

The use of "type="null"" in the function arguments indicates that the type is optional.

 

if ($type == '') 
{
  $type = $this->alertTypes[0];
}

If it isn't included, we will use the first (default) alert type (which is useful to help shorten the code you have to write if you are constantly using the default type).

 

$_SESSION[$type][] = $value;

The idea behind this function was that I wanted to be able to store unlimited alerts of the same type. For example, this could be useful if you are validating a form, and you want a separate alert of the same type displayed for each form field that wasn't filled in. Like BeeDev said above, this line adds a node at the end of the current array, which makes it easy to add items, rather than indicating a specific spot in the array (for example, "$_SESSION[$type][0]") and having to keep track of how many items are currently in the array.

 

Hopefully this is detailed enough. :) If you have any other questions regarding other functionality, let me know.

Link to comment
Share on other sites

Thanks for your reply :D Still staring at the codes to understand the real process. Hopefully the thing will clicks soon :P

 

Ok when we say :

 

$type = $this->alertTypes[0];

 

What I understood right now is, we want to replace the $type with $this->alertTypes and put the node 0 to it. Like this :

 

$type[0]

 

Am I right?

Link to comment
Share on other sites

I wouldn't say "replace", but that line sets the value of a temporary $type variable to the value of the first element in the alertTypes array (at least in my code, the default is "success".)

 

Assuming you are following my code, the alertTypes variable is set at the top of models/m_template.php in this line:

 

private $alertTypes = array('success', 'alert', 'error');

Link to comment
Share on other sites

Really? What I saw in the video you only set the variable like this :

 

private $alertTypes;

 

Or maybe I just missed seeing :P

 

Ok enough for that part. Still not clear but better than before.

 

I have one more question. This one is about getAlert()

 

Consider this line :

 

if (isset($_SESSION[$alert]))

 

Is it the session come from this line :

 

$_SESSION[$type][] = $value;

 

If not, I need your explanation. Sorry to trouble you, Ben.

Link to comment
Share on other sites

Really? What I saw in the video you only set the variable like this

Sorry, you are right. That variable is set in the init.php file. I must have been looking at an old version of the files.

 

Consider this line :

 

if (isset($_SESSION[$alert]))

 

Is it the session come from this line :

 

$_SESSION[$type][] = $value;

Correct. Alerts are set with the setAlert() function. The getAlerts() function loops through any of the available alert types (that were initially set in the init.php file by calling the setAlertTypes() function) and displays them if there are any to display.

Link to comment
Share on other sites

Thanks for your explanation, Ben :clap:

 

One more thing to clear and this is because I always see the same variable in your code. So I get confuse whether it is same variable or the other one.

 

Consider this line

 

$_SESSION[$type][] = $value;

 

and

 

foreach($_SESSION[$alert] as $value)

 

Both variable is same which is $value and both is an assignment to the same session (you mention earlier post). So this is just like confuse me a little bit :P

Link to comment
Share on other sites

In both those cases, $value is a temporary variable that exists only within the specific function it is used. I just used that as a consistent way of naming a temporary variable -- they aren't related to each other in any way. In the first example:

 

function setAlert($value, $type = null)
{
	if ($type == '') { $type = $this->alertTypes[0]; }
	$_SESSION[$type][] = $value;
}

$value is simply a temporary variable that holds whatever is input when setAlert() is called. Once the function ends, the variable disappears.

 

Similarly, in the second example:

 

foreach($_SESSION[$alert] as $value)
			{
				$data .= '<li class="'. $alert .'">' . $value . '</li>';
			}
			unset($_SESSION[$alert]);

$value is a temporary variable that exists only within the foreach loop. Once the foreach loop ends, the variable no longer exists.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...