Tag: variables Archives Not Just A Random Blog

08/09/2011

JavaScript Functions With an Undefined Number of Arguments

by Cyle — Categories: javascript, Web Development — Tags: , , Leave a comment

In JavaScript there is an easy way to retrieve all the arguments that were passed to the function even if we do not know how many were passed. There is an arguments array-LIKE object. Notice it is not an array, but similar to one. We cannot join(), pop(), push(), slice(), etc. like we can with an array, but we can retrieve the values from the object like we do an array.

Example
<script type="text/javascript">
function sum() {
    var total = 0;
    for (var i = 0, len = arguments.length; i < len; ++i) {
        total += arguments[i];
    }
    return total;
}
 
sum(1, 2, 3, 4) // returns 10
 
</script>

08/09/2011

Default Values In Functions

by Cyle — Categories: javascript, php, Web Development — Tags: , , , Leave a comment

We can set a default value for an argument passed to a function so that if an argument is not passed, there will still be a value for it. This can be used when a common value is required for a function and you are wanting to leave it out when calling the function.

 

In PHP

This can be done in PHP by setting an argument to a value when declaring the function.

Example
< ?php
function testFunction($var1, $var2 = true) { 
  // do something here...
  if ($var2=='true'){echo $var1;}
}
 
testFunction('my message'); // will print 'my message'
testFunction('my message',false); // will not print anything
?>

 

In Javascript

Doing the same thing in Javascript is a little more difficult. There are two methods that I am familiar with. The first is not 100% bullet proof and will not work under certain situations. Read More » »

08/09/2011

Include Files Can Have A Return Value

by Cyle — Categories: php, Web Development — Tags: , , Leave a comment

Over the years of programming, I just now figured this out. Include files can have a return value that you can assign to a variable. It can be very helpful especially when you separate your code into include files. For this example I will return an array from an include file, but any variable can be returned using the return command.

Include File (config.php)
< ?php
// do something here...
return array(
    'db' => array(
        'host' => 'example.com',
        'user' => 'user1',
        // etc...
    ),
    // etc...
);
?>

Read More » »

06/21/2011

PHP’s Global Server Variables

by Cyle — Categories: php — Tags: , , , , 1 Comment

Most all web servers pass an array containing a lot of helpful information such as headers, host information, and more. There are many elements in the array, but I am only going to discuss the ones I use most. The $_SERVER array must be called inside the script and not on a command line. Let’s start with an example:

1
2
3
<?php
echo $_SERVER['SERVER_NAME'];
?>

If the above example was run on this blog, it would output:

njarb.com

Below are the elements I use most in $_SERVER array:

‘SERVER_ADDR’: The IP address of the server that is currently running the script (usually the web host server).

‘SERVER_NAME’: The name of the server that is currently running the script.

‘REQUEST_METHOD’: The method used to access this page. (‘GET’, ‘POST’, ‘PUT’, ‘HEAD’)

‘REQUEST_TIME’: The time of the start of the request. (Available in PHP 5+)

‘DOCUMENT_ROOT’: The document root directory set by the server’s configuration file which the current script is running.

‘HTTP_REFERER’: The address of the page that referred him/her to your webpage. This can be modified by most browsers, so it should not be trusted, but is good information.

‘HTTP_USER_AGENT’: The browser information of the user (Example: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586))

‘HTTPS’: This element is set to a non-empty value if the script was queried through the HTTPS protocol.

‘REMOTE_ADDR’: The IP address of the user viewing the page.

‘REMOTE_HOST’: The host name of the user viewing this page.

‘REQUEST_URI’: The original URL that was requested before any redirects sent the user to the final landing page.

‘REDIRECT_STATUS’: The redirect status (200 = OK, 404 = not found, 403 = forbidden, etc.)

Example:

Below is an example using all the mentioned elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
echo '<b>SERVER_ADDR</b>: '.$_SERVER['SERVER_ADDR'];
echo '<br /><br /><b>SERVER_NAME</b>: '.$_SERVER['SERVER_NAME'];
echo '<br /><b>REQUEST_METHOD</b>: '.$_SERVER['REQUEST_METHOD'];
echo '<br /><b>REQUEST_TIME</b>: '.$_SERVER['REQUEST_TIME'];
echo '<br /><b>DOCUMENT_ROOT</b>: '.$_SERVER['DOCUMENT_ROOT'];
echo '<br /><b>HTTP_REFERER</b>: '.$_SERVER['HTTP_REFERER'];
echo '<br /><b>HTTP_USER_AGENT</b>: '.$_SERVER['HTTP_USER_AGENT'];
echo '<br /><b>HTTPS</b>: '.$_SERVER['HTTPS'];
echo '<br /><b>REMOTE_ADDR</b>: '.$_SERVER['REMOTE_ADDR'];
echo '<br /><b>REMOTE_HOST</b>: '.$_SERVER['REMOTE_HOST'];
echo '<br /><b>REQUEST_URI</b>: '.$_SERVER['REQUEST_URI'];
echo '<br /><b>REDIRECT STATUS</b>: '.$_SERVER['REDIRECT_STATUS'];
?>

The output of the above code for this current session is:

SERVER_ADDR: 174.120.174.112
SERVER_NAME: njarb.com
REQUEST_METHOD: GET
REQUEST_TIME: 1369402943
DOCUMENT_ROOT: /home/cconoly/public_html/njarb.com
HTTP_REFERER:
HTTP_USER_AGENT: CCBot/2.0
HTTPS:
REMOTE_ADDR: 50.17.109.248
REMOTE_HOST:
REQUEST_URI: /tag/variables/
REDIRECT STATUS: 200

All of the $_SERVER elements and more about each property can be found at php.net’s PHP $_SERVER – Manual

06/21/2011

PHP Variable Variables

by Cyle — Categories: php — Tags: , Leave a comment

Sounds confusing, right? It sure can be. In some cases a dynamic variable (when the variable name changes) may be needed. A variable variable is a variable whose name is determined by another variable. Look at the example below:

1
2
3
4
$varname = 'myvariable';
$myvariable = 'The data I really want';
 
echo $$varname;

The above code outputs:

The data I really want

Notice how we used two $ instead of just one. $$varname = $[content of $varname] = $myvariable.

Here’s another example using variable variables, which is also known as a family of variables:

1
2
3
4
5
for ($i = 0; $i <= 4; $i++) {
  ${myvar.$i} = 'content';
}   
 
echo "$myvar1, $myvar2, $myvar3, $myvar4, $myvar5";

The output would be:

content content content content content

Notice how we used the { } brackets. ${$myvar} is the same as $$myvar, but when we add a string or anything to the variable name we need to include the brackets. The period in the last example is a string concatenation operator that combines the string myvar with the variable $i.

So… Should I Use Variable Variables?

Yes and No. Variable variables can make coding much more flexible. Let’s say a variable name needed to be changed, like $address to $streetNumber. You would have to change every line that contained $address, but if it were a variable variable, we would only have to change one line. I often recommend to not use variable variables a whole lot in a single piece of code because it can get very confusing when you come back later to it and see $$myvar and cannot remember what $myvar‘s value was. Be sure to comment the code to make it much easier to understand later when you forget what the variable variables are actually pointing to.

© 2013 Not Just A Random Blog All rights reserved - Theme by ([][]) TwoBeers