Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
| function | description |
|---|---|
glob |
returns an array of all file names that match a given pattern (returns a file path and name, such as "foo/bar/myfile.txt")
|
scandir |
returns an array of all file names in a given directory (returns just the file names, such as "myfile.txt")
|
glob can accept a general path with the * wildcard character (more powerful)
glob example
# reverse all poems in the poetry directory
$poems = glob("poetry/poem*.dat");
foreach ($poems as $poemfile) {
$text = file_get_contents($poemfile);
file_put_contents($poemfile, strrev($text));
print "I just reversed " . basename($poemfile) . "\n";
}
glob can match a "wildcard" path with the * character
glob("foo/bar/*.doc") returns all .doc files in the foo/bar subdirectory
glob("food*") returns all files whose names begin with "food"
basename function strips any leading directory from a file path
basename("foo/bar/baz.txt") returns "baz.txt"
scandir example
<ul>
<?php foreach (scandir("taxes/old") as $filename) { ?>
<li>I found a file: <?= $filename ?></li>
<?php } ?>
</ul>
scandir includes current directory (".") and parent ("..") in the array
basename with scandir; returns file names only without directory
# reverse a file
$text = file_get_contents("poem.txt");
$text = strrev($text);
file_put_contents("poem.txt", $text);
file_get_contents returns entire contents of a file as a string
file_put_contents writes a string into a file, replacing its old contents
# add a line to a file
$new_text = "P.S. ILY, GTG TTYL!~";
file_put_contents("poem.txt", $new_text, FILE_APPEND);
| old contents | new contents |
|---|---|
Roses are red, Violets are blue. All my base, Are belong to you. |
Roses are red, Violets are blue. All my base, Are belong to you. P.S. ILY, GTG TTYL!~ |
file_put_contents can be called with an optional third parameter to append (add to the end) rather than overwriteCheck out this awesome photo gallery
URL?name=value&name=value...
http://www.google.com/search?q=Obama http://example.com/student_login.php?username=stepp&id=1234567
username has value stepp, and sid has value 1234567$_GET,
$_POST
$user_name = $_GET["username"];
$id_number = (int) $_GET["id"];
$eats_meat = FALSE;
if (isset($_GET["meat"])) {
$eats_meat = TRUE;
}
$_GET["parameter name"] or $_POST["parameter name"] returns a GET/POST parameter's value as a string
http://....?name=value&name=value are GET parametersisset
<form>
<form action="destination URL"> form controls </form>
action attribute gives the URL of the page that will process this form's dataaction's URL
<form action="http://www.google.com/search"> <div> Let's search Google: <input name="q" /> <input type="submit" /> </div> </form>
div<input>
<!-- 'q' happens to be the name of Google's required parameter -->
<input type="text" name="q" value="Colbert Report" />
<input type="submit" value="Booyah!" />
input element is used to create many UI controls
name attribute specifies name of query parameter to pass to servertype can be button, checkbox, file, hidden, password, radio, reset, submit, text, ...value attribute specifies control's initial text<input>
<input type="text" size="10" maxlength="8" /> NetID <br /> <input type="password" size="16" /> Password <input type="submit" value="Log In" />
input attributes: disabled, maxlength, readonly, size, valuesize attribute controls onscreen width of text fieldmaxlength limits how many characters user is able to type into fieldplaceholder attribute specifies suggestion textWe want to write a simple HTML form that gathers the necessary information to display a student's grade to them. Here is some starter code.
<textarea>
a multi-line text input area (inline)
<textarea rows="4" cols="20"> Type your comments here. </textarea>
textarea tag (optional)rows and cols attributes specify height/width in charactersreadonly attribute means text cannot be modified<input>
yes/no choices that can be checked and unchecked (inline)
<input type="checkbox" name="lettuce" /> Lettuce <input type="checkbox" name="tomato" checked /> Tomato <input type="checkbox" name="pickles" checked /> Pickles
on:
http://webster.cs.washington.edu/params.php?tomato=on&pickles=on
checked attribute in HTML to initially check the box, the empty value syntax is equivalent to checked=""<input>
sets of mutually exclusive choices (inline)
<input type="radio" name="cc" value="visa" checked /> Visa <input type="radio" name="cc" value="mastercard" /> MasterCard <input type="radio" name="cc" value="amex" /> American Express
name attribute (only one can be checked at a time)value for each one or else it will be sent as value on<select>,
<option>
menus of choices that collapse and expand (inline)
<select name="favoritecharacter"> <option>Jerry</option> <option>George</option> <option selected>Kramer</option> <option>Elaine</option> </select>
option element represents each choiceselect optional attributes: disabled, multiple, sizeselected attribute sets which one is initially chosen<label>
<label><input type="radio" name="cc" value="visa" checked="checked" /> Visa</label> <label><input type="radio" name="cc" value="mastercard" /> MasterCard</label> <label><input type="radio" name="cc" value="amex" /> American Express</label>
label element can be targeted by CSS style rules<select> for lists<select name="favoritecharacter[]" size="3" multiple> <option>Jerry</option> <option>George</option> <option>Kramer</option> <option>Elaine</option> <option selected>Newman</option> </select>
multiple attribute allows selecting multiple items with shift- or ctrl-click
[] if you allow multiple selections
option tags can be set to be initially selected<optgroup>
<select name="favoritecharacter"> <optgroup label="Major Characters"> <option>Jerry</option> <option>George</option> <option>Kramer</option> <option>Elaine</option> </optgroup> <optgroup label="Minor Characters"> <option>Newman</option> <option>Susan</option> </optgroup> </select>
optgroups?Name: <input type="text" name="name" /> <br /> Food: <input type="text" name="meal" value="pizza" /> <br /> <label>Meat? <input type="checkbox" name="meat" /></label> <br /> <input type="reset" />
value attributeI changed the form's HTML code ... but when I refresh, the page doesn't update!
<input type="text" name="username" /> Name <br /> <input type="text" name="sid" /> SID <br /> <input type="hidden" name="school" value="UW" /> <input type="hidden" name="year" value="2048" />
<fieldset>,
<legend>
groups of input fields with optional caption (block)
<fieldset> <legend>Credit cards:</legend> <input type="radio" name="cc" value="visa" checked="checked" /> Visa <input type="radio" name="cc" value="mastercard" /> MasterCard <input type="radio" name="cc" value="amex" /> American Express </fieldset>
fieldset groups related input fields, adds a border; legend supplies a captionelement[attribute="value"] { property : value; property : value; ... property : value; }
input[type="text"] {
background-color: yellow;
font-weight: bold;
}
input)