LIS 390W1A - Lab 15: Variables, Arrays & Basic Functions

Legend

 
= Javascript
 
= PHP

Javascript Exercise

For this exercise, you should dowload (right-click, "save as...") js_ex_2.html and js_ex_2.css, and use them to follow along. Before we begin this exercise, there are several things to keep in mind from last lecture:

  • Javascript is case sensitive. This means that it is very important that you use uppercase and lowercase letters consistently. If you are using predefined javascript functions, then be sure to get not only the spelling right, but the cases right.
  • When you use javascript in your pages, be sure to use the following form in your script tags:

    <script type="text/javascript">
    /* <![CDATA[ */
    	// Insert javascript here.
    /* ]]> */
    </script>
    		

    The justification for this is covered nicely in the article Javascript and XHTML. The basic idea is that if you parse the data within the script tags (i.e. consider it to be PCDATA = "parsed character data" - which is the default), then the browser or validator may/will (correctly) interpret the HTML tags within the script tags as HTML tags, rather than as character data that is to be used by the javascript, and the javascript alone. Thus, you want to explicitly identify the data within the script tags as character data (CDATA), not parsed character data (PCDATA). The "/*" and "*/" are the begin and end comment tags for javascript, which is important so that the javascript interpreter in your browser doesn't interpret the "<![CDATA[" or the "]]>" as javascript code, which it is not.

  • Javascript is technically a scripting language. For a really good account of the difference between scripting languages and programming languages, read the blog post: Scripting vs. programming: is there a difference?

    The short answer is that there's not much difference, but one thing to note is that javascript is processed each time a page is loaded. The significance of this for javascript as opposed to other scripting languages, however, is that it is processed by the browser, not by a program which processes it before it gets to the browser. As a result, debugging javascript can often be extremely difficult, because the only output you can see is what gets presented on the screen. If you view source, you still can't see the DOM manipulation, etc., which your javascript might (almost certainly is) doing.

    Another difference is that you can't create a "program" with javascript. This isn't such a big deal, but it means that you can't do certain things with javascript which you can do with other languages. Thus, while both might be turing-complete, this only means that you can create identical kinds of functionality. It doesn't mean that you can address, point to, refer, or affect the same (kinds of) objects with whatever functionality you create.

    Lastly, you should realize that the term "programming language" really has two senses. One is the specific sense described in the blog post referenced above, as a contrasting category to "scripting language". The other is as a super-category that covers all kinds of (turing-complete, computational) languages, including both scripting languages and programming languages in the specific sense of "programming language". Thus, when people talk about "web programming", they are using the more general sense of programming. From now on, unless specifically contrasting scripting with programming languages, I will be using "programming" in its more general sense.

  • Because javascript debugging can be a huge pain, if you are doing anything serious in javascript, I recommend you install the Firebug firefox extension. This is a debugging tool that is integrated into firefox to help you find the errors that can occur.

PHP Exercise

For the PHP portion of this lab, there is no separate exercise. Instead, we'll just go over what the constructs look like in PHP, and how they differ from those in javascript.

PHP Variables

Declaring the variables. In PHP, the variables are declared when first used. Because there is no problem with scoping as there is in javascript, there is no reason to explicitly declare them in PHP. Thus:

<?php
	$x = 1; // defines $x as an integer
	$chr = "c"; // defines $chr as a character
	$str = "string"; // defines $str as a string
?>
	

You can however explicitly typecast variables, thereby using PHP as if it were a strongly-typed language:

<?php
	$tstr = (string) "Strongly typed string"; // strong typing as a string
	$y = (int) 42; // strong typing as an integer
?>
	

Legal types (copied from php.net) include:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (binary) - cast to binary string (PHP 5.2.1+)
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL (PHP 5)

Like javascript a php variable can be undefined if it is not set to a value. However, it will not display the text "undefined" if printed, it will simply not display anything at all (as if it were an empty string). It can create errors when used in computation, however undefined variables are typically set to zero in numerical computations.

PHP Operators work much the same as they do in javascript, however they are not overloaded. Thus, the "+" never serves to concatenate strings. Instead "." is the string concatenator:

<?php
	$cstr = "hello " . "world";
?>
	

PHP Arrays

PHP Arrays are handled similarly to javascript. However, there are more ways to define an array. For example:

<?php
$students = array();
$students[0]  = "Jin Ha";
$students[1]  = "Dinesh";
$students[2]  = "Richard";
$students[3]  = "Anna";
$students[4]  = "Jenny";
$students[5]  = "Vandana";
$students[6]  = "Wei";
$students[7]  = "Karen";
$students[8]  = "Xin";
$students[9]  = "Tom";
$students[10] = "Cindy";
$students[11] = "Anatoliy";
$students[12] = "Ingbert";
$students[13] = "Ellen";
$students[14] = "Ellen";
$students[15] = "Wu";
$students[16] = "Melissa";
$students[17] = "Oksana";

/* or: */

$study = array("hard" => "pass", "not" => "fail", 42 => true);
?>
	

PHP Functions - Basic

PHP Functions are also handled similarly to javascript (though scoping is handled differently):

<?php
function printvar($x)
{
    echo "<div style=\"background:red;\">$x</div>";
    return $x;
}
?>
	

Submission

Your assignment for this class is simply to email me with any questions you have so far about the javascript and php material.

Grading Criteria

Out of 15 points total.

  • That you sent me an email: 15 points