▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP –– Loops
written by: admin


Date Written: 8/11/07 Last Updated: 4/18/10

foreach and while are the two type of loops php uses.


while

A loop where every even numbered value in an array is converted to uppercase and the odd ones have only the first letter of the value capitalized.

<?php $terms=array(ae,be,ce,de,ee,fe,ge,he,ie,je,ke,le,me,ne,oe,pe,qe,re,
se,te,ue,ve,we,xe,ye,ze);
$t=0;$j=count($terms);
while ($t<$j){
if ($t%2==0) $terms[$t]=ucwords($terms[$t]);
else $terms[$t]=strtoupper($terms[$t]);
echo"$terms[$t]<br>";
$t++;
}
?>




Capitalize all of the letters in an array.
example 1
If you were to convert every value in an array the same way you would probably want to use a foreach().
<?php $terms=array(ae,be,ce,de,ee,fe,ge,he,ie,je,ke,le,me,ne,oe,pe,qe,re,
se,te,ue,ve,we,xe,ye,ze);
foreach ($terms as $terms1=>$value){
$term[]=strtoupper($value);
}$terms=$term;echo "$terms[5]";
?>

This will produce:
FE
example 2
This script does the same thing as example 2 above, but better.
<?php $terms=array(ae,be,ce,de,ee,fe,ge,he,ie,je,ke,le,me,ne,oe,pe,qe,re,
se,te,ue,ve,we,xe,ye,ze);
foreach ($terms as &$value){
$value=strtoupper($value);
}
echo $terms[5];
?>

Capitalize the first letter of every sentence in a string.
<?php
$sample="this is me.  this is sentence two.  hi.";
$sample=explode('.  ',$sample);
foreach ($sample as $sentence=>$val) {$val1[]=ucfirst($val);}
$sample=implode(". ",$val1);
echo"$sample";
?>


TAGS: php
copyright 2005–2024