Author: techfox9

php introduction notes – part 1..

Tuesday, July 1st, 2008 @ 10:39 pm

Basic things about php..

From

https://www6.software.ibm.com/developerworks/education/os-phptut1/index.html

Variables

A variable is a placeholder for data. You can assign a value to it, and from then on, any time PHP encounters your variable, it will use the value instead. For example, change your page to read:


<html>
<title>Workflow Registration</title>
<body>
<p>You entered:</p>

<?php
$username = “tyler”;
$password = “mypassword”;

echo “<p>Username = ” . $username . “</p>”;
echo “<p>Password = ” . $password . “</p>”;
?>

</body>
</html>

Constants

<?php
define(“PAGE_TITLE”, “Workflow Registration”);
?>

<html>
<title><?php= PAGE_TITLE ?></title>

Accessing form data

<body>
<p>You entered:</p>

<?php
$username = $_GET[‘name’];
$password = $_GET[‘pword’];

echo “<p>Username = ” . $username . “</p>”;
echo “<p>Password = ” . $password . “</p>”;
?>

</body>
</html>

Arrays


$formnames = array(“name”, “email”, “pword”);
echo “0=”.$formnames[0].”<br />”;
echo “1=”.$formnames[1].”<br />”;
echo “2=”.$formnames[2].”<br />”;

Notice that the first value has an index of 0, rather than 1. Notice also that you specified which value you wanted by adding the index in brackets after the name of the array variable. This action is similar to the way in which you access the form values, and that’s no accident. The $_GET variable is a special kind of an array called an associative array, which means that instead of a numeric index, each value has a key.

When you submit the form, you’re essentially creating an array like so:


$_GET = array(“name” => “roadnick”,
“email” => “ibmquestions@nicholaschase.com”,
“pword” => “supersecretpassword”);

Associative arrays

<?php
$form_names = array_keys($_GET);
$form_values = array_values($_GET);

echo “” . $form_names[0] . ” = ” . $form_values[0] . “”;
echo “” . $form_names[1] . ” = ” . $form_values[1] . “”;
echo “” . $form_names[2] . ” = ” . $form_values[2] . “”;
?>

For-next loop


for ($i = 0; $i < 10; $i++) { echo $i . " "; }


<?php
$form_names = array_keys($_GET);
$form_values = array_values($_GET);

for ($i = 0; $i < sizeof($_GET); $i++) {
echo “<p>”.$form_names[$i].” = ” . $form_values[$i] . “</p>”;
}
?>

Foreach loop

<?php
foreach ($_GET as $value) {
echo “<p>” . $value . “</p>”;
}
?>

<?php

foreach ($_GET as $key=>$value) {
echo “<p>”.$key.” = ” . $value . “</p>”;
}
?>

Using POST

<?php

foreach ($_POST as $key=>$value) {
echo “<p>”.$key.” = ” . $value . “</p>”;
}

$passwords = $_POST[“pword”];
echo “First password = “.$passwords[0];
echo “<br />”;
echo “Second password = “.$passwords[1];
?>

php, Web


 


Comments are closed.