Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

, Web Scripting (PHP) , Write a script that uses at - least six string functions

ID: 3750826 • Letter: #

Question

,

Web Scripting (PHP)

,

Write a script that uses at

-

least six string functions. You have to use 3 string functions that is not

discussed in the book (

www.php.net

) and

the other 3 could be from the book. Inside the script,

explain what the function is doing.

Output:

The initial string displays:

It is becoming increasingly clear that we are responsible for warming of the

Earth primarily due to the burning of f

ossil fuels. Predicting the

implications of this or how the picture will change in the future are big

challenges for scientists and today's report by the IPCC, whilst recognising

uncertainties, gives us the best possible insight into what may lay ahead.

B

y using the <<.............>> string function, now the string is converted to

It is becoming

increasingly clear that

we are responsible for

warming of the Earth

primarily due to the

burning of fossil fuels.

Predicting the

implications of this or

how the picture w

ill

change in the future are

big challenges for

scientists and today's

report by the IPCC,

whilst recognising

uncertainties, gives us

the best possible insight

into what may lay ahead.

Purpose: Comprehend and demonstrate the use of String functions

as discussed in class

.

2.

According to the U.S. Census

Bureau, the 12 largest American cities (by population) in 2005 were as

follows:

(the numbers are not actual numbers)

New Jersey, NJ (9,008,278 people)

Los Angeles, CA (3,694,410)

Chicago, IL (2,985,016)

Houston, TX (1,845,631)

Philadelphia, PA (1,517,550)

Phoenix, AZ (1,321,045)

San Diego, CA (1,283,400)

Dallas, TX (1,188,580)

San Antonio, TX (1,198,646)

Detroit, MI (951,270)

Georgia, GA (378, 457)

New York, NY (678, 4567)

Write a script that d

efine an array (or arrays) that holds this information about loc

ations and population.

Print a table of locations and population information that includes the total population in all 12 cities.

Modify your solution so that the rows in result table are ordered by population.

Then modify your solution so that

the rows are ordered by city name.

Explanation / Answer

ANSWER:

2.

Here is your php script to accomplish all three requirements. Let me know via comment if you need further help:

<html>

<head><title>PHP Arrays</title></head>

<body>

<?php

function print_table($cities)

{

echo '<table border="2" cellpadding="5"><tr><th>City Name</th><th>State</th><th>Population</th></tr>';

foreach($cities as $obj)

{

echo '<tr>';

echo '<td>' . $obj['name'] . '</td><td>' . $obj['state'] . '</td><td>' . $obj['population'] . '</td>';

echo '</tr>';

}

echo '</table>';

}

$cities = array();

$cities[] = array('name' => 'New Jersey', 'state' => 'NJ', 'population' => 9008278);

$cities[] = array('name' => 'Los Angeles', 'state' => 'CA', 'population' => 3694410);

$cities[] = array('name' => 'Chicago', 'state' => 'IL', 'population' => 2985016);

$cities[] = array('name' => 'Houston', 'state' => 'TX', 'population' => 1845631);

$cities[] = array('name' => 'Philadelphia', 'state' => 'PA', 'population' => 1517550);

$cities[] = array('name' => 'Phoenix', 'state' => 'AZ', 'population' => 1321045);

$cities[] = array('name' => 'San Diego', 'state' => 'CA', 'population' => 1283400);

$cities[] = array('name' => 'Dallas', 'state' => 'TX', 'population' => 1188580);

$cities[] = array('name' => 'San Antonio', 'state' => 'TX', 'population' => 1198646);

$cities[] = array('name' => 'Detroit', 'state' => 'MI', 'population' => 951270);

$cities[] = array('name' => 'Georgia', 'state' => 'GA', 'population' => 378457);

$cities[] = array('name' => 'New York', 'state' => 'NY', 'population' => 6784567);

echo '<h3>Original Data: </h3>';

print_table($cities);

# Calculate Total:

$total = 0;

foreach($cities as $obj)

{

$total += $obj['population'];

}

echo 'The total population is: ' . $total . '.';

echo '<h3>Ordered by population: </h3>';

foreach ($cities as $key => $row)

{

$population[$key] = $row['population'];

}

array_multisort($population, SORT_DESC, $cities);

print_table($cities);

echo '<h3>Ordered by city name: </h3>';

foreach ($cities as $key => $row)

{

$city_name[$key] = $row['name'];

}

array_multisort($city_name, SORT_ASC, $cities);

print_table($cities);

?>

</body>

</html>