1. Hi, , onclick , Hello to you, too! ( !).
2. , : document.write("Hello, world.");. HTML-. , .
3. JavaScript.
4. HTML-, . .
5. JavaScript, , Web-.
6. JavaScript, Web- <hl > .
7. , ( ) , () . .
8. , . URL .
9. , :
window.status = "Welcome to my Web page.";
document.write("<hl>Welcome to my Web page.</hl>")
, .
10. : , .
11. body , , prompt ().
<html> <head>
<script type=text/javascript">
function askName() {
var name = prompt ("What is your name, please?11,"")
return name
}
</script>
</head>
<body>
<script type="text/javascript">.
document.write("Welcome to my web page, " + askName() + ".")
</script>
</body>
</html>
onclick .
12. URL href location.
<html> <head>
<script type="text/javascript">
function showLocation() {
alert("This page is at: " + location.href);
}
</script> </head>
<body onload="showLocation()"> Bu, be, by.
</body> </html>
onclick .
13. JavaScript.
14.
15. JavaScript
16. .
17. .
|
|
18. .
19. .
20. -.
21. (, , ).
22. .
23. . , . .
24. -.
25. -.
26. -.
27. : .
28. JavaScript.
29. .
30. .
31.
32. . .
33. JavaScript ( -).
34. Web-
35. . .
36. .
37. Web- , , .
38. . Web- 7- 5- .
39. .
40.
41. .
42.
43. JavaScript
44. -------2 +------ +-------- =0
45. cookie.
46. JavaScript.
47. DOM.
48. DOM AJAX.
49. : , , , HTML- , .
50. : KOI8-R, WINDOWS-1251, UNICODE.
51. .
52. , :
)
b)
c) HTML
d)
52. (, , , , ). :
{// :
name //();
birthYear //( );
country //( , USA);
alive //( , true).
}
:
setCountry() setBirthYear .
Spisok:
{ supermans // ;
length // .
}
:
Add () ( ), Remove() ( ), Replace() ( ). 5-10 .
, ( insertRow insertCell, MSDN). : , , , , .
, ( ), : , ( ).
|
|
: , . , - , . (, ), . ( HTML- 9650 9660).
5. PHP
PHP , . PHP 1994 . , PHP Personal Home Page Tools. PHP - . PHP , . PHP , , , , . . .
PHP.
Web- , , : , , , , .. PHP Web-, . C PHP C++, Java. , PHP, . PHP . . PHP , , , cookies, . PHP . , PHP MySQL.
PHP POP3 SMTP, TCP/IP, HTTP , .
Firefox. PHP Adobe DreamWeaver CS5 (JQuery API). Java PHP- Eclipse Eclipse JSEclipse Aptana Studio.
PHP web-Apache, PHP MySql, . Total Comander, PHP . DNS - . PHP (localhost)
PHP
, PHP. , , PHP, HTM, HTML PHTML. WEB- Apache : AddType. PHP Apache. - Apache, MySql. www.localhost/name.php.
PHP :
<?php // xml
//php
?>
php , <? ?>. .php html .
|
|
PHP:
<!pr0.php -->
<html>
<head> </head>
<script language="php">
print "Hello,world- PHP<BR>";
</script>
<body>
PHP Html<BR>
<?
print "Hello, to you - PHP<BR>";
?>
<?php
echo " PHP - PHP<BR><HR>";
phpinfo(); //
// php
?>
</body>
</html>
:
Hello,world- PHP
PHP Html
Hello, to you - PHP
PHP - PHP
, PHP, echo, HTML . echo (string arg1, string arg2, string argn) . PHP print, echo(). print() echo() . : printf(format, arglist).
PHP :
<?php //pr1.php
$exp=true;
if ($exp) {
?>
<strong> .</strong>
<?php
}
else {
?>
<strong> .</strong>
<?php
}
printf("<br> %s", " ");
?>
:
.
.
PHP ?>, , . PHP , echo() print().
PHP 4 : boolean ( true false), integer ( , , ), float ( double), string( ) : array object. : resource NULL. , 0x.
<?php //pr01.php
$a = 10;
$b = 020;//
$c = 0x41;//
$d=3.62e+2;
printf("%d %o %c %f",$a,$b,$c,$d);
?>
:
10 20 A 362.000000
%d , %o , %c , %f - .
, var_dump(). , gettype(). , is_ type().
$. PHP , . , $p , $p . $p , $p .
PHP . , define() $. :
|
|
<?php //pr2.php
define("d",5,true);
$a = " ";
$b = " PHP";
$c = 4;
$c++;
echo "$a$b$c"; // - PHP5
print d;// 5
$p=NULL;
print var_dump($p);
print gettype($a);
?>
:
PHP55NULL string
PHP , C++:
/* */
//
# .
, . , PHP Unicode. Unicode utf8_encode() utf8_decode(). Unicode PHP6.
PHP . . PHP : . , . , , , . , , PHP.
<?php //pr3.php
$name = 'Domine?';
$name_1 = "Quo vadis, $name";
$name_2 = ' Quo vadis, $name';
echo "$name_1"; // - Quo vadis, Domine?
echo "$name_2"; // - Quo vadis, $name
?>
PHP , : \n - , \r- , \t- , \\- , \$- , \\"- , \ .
:
$s=\x41- a ;
$st= \ \ ;
, .
- heredoc- ("<<<"). <<< , , , . . , , (;). , . . , \r. Heredoc- , , . , heredoc, - . heredoc-:
<?php
$name = "Max";
$str = <<<DEMO
Hello $name! <br/>
This is a demo message
with heredoc.
DEMO;
echo $str;
?>
:
Hello Max! This is a demo message with heredoc.
, heredoc, . : . ($), , , . , .
<?php
$beer = '';
echo "$beer's taste is great"; /* , 's - */
echo "He drank some $beers"; /* , 's' */
echo "He drank some ${beer}s"; //
echo "He drank some {$beer}s"; //
?>
() .
, , . , , { }. '{', $ {. :
<?php
//
error_reporting(E_ALL);
$great = 'fantastic';
// , : This is { fantastic}
echo "This is { $great}";
// , : This is fantastic
echo "This is {$great}";
echo "This is ${great}";
|
|
//
echo " {$square->width}00 .";
//
echo " : {$arr[4][3]}";
// , $foo[bar]
// . , - ,
// PHP foo,
// E_NOTICE ( ).
echo " : {$arr[foo][3]}";
// . ,
//
echo " : {$arr['foo'][3]}";
// .
echo " : ". $arr['foo'][3];
echo " {$obj->values[3]->name}";
echo " $name: {${$name}}";
?>
PHP (), Perl. .
<?php
echo dir;?>
PHP , .
0:
$massiv[0] = 'name';
$massiv[1] = 'phone';
() -.
$new_massiv['name'] = 'Valera';
$new_massiv['email'] = '[email protected]';
: , array(). , , . .
$massiv[] = ""; //$massiv[2] = " "
$massiv[] = "";//$massiv[3] = " "
array():
$massiv = array('one','two','three');
:
$new_massiv = array('name' => 'nobody', 'email' => '[email protected]');
=>. :
<?php //pr45
$massiv = array('one','two','three');
$massiv[5]="5";
$massiv[6]="6";
$massiv[]="7";
print_r($massiv);
$new_massiv = array('name' => 'nobody', 'email' => '[email protected]');
print_r($new_massiv);
print $new_massiv['name']
?>
:
Array ([0] => one [1] => two [2] => three
[5] => 5 [6] => 6 [7] => 7)
Array ([name] => nobody [email] => [email protected])
nobody
print_r() .
PHP . , PHP. PHP:
$_GET[]
$_POST[]
$_REQUEST[]
$_COOKIE[]
$_FILES[]
$_SERVER[]
$_ENV[]
$_SESSION[]
$_SERVER
<?php //pr46
//print_r($_GET);
//print_r($_POST);
//print_r($_REQUEST);
//print_r($_COOKIE);
//print_r($_FILES);
print_r($_SERVER);
//print_r($_ENV);
//print_r($_SESSION);
?>
:
Array
(
[COMSPEC] => C:\WINDOWS\system32\cmd.exe
[DOCUMENT_ROOT] => z:/home/localhost/www
[HTTP_ACCEPT] => image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_ACCEPT_LANGUAGE] => ru
[HTTP_CONNECTION] => Keep-Alive
[HTTP_HOST] => localhost
[HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)
[PATH] => \usr\local\ImageMagick;\usr\local\php5; C:\PROGRA~1\Borland\CBUILD~1\Bin; C:\PROGRA~1\Borland\CBUILD~1\Projects\Bpl; C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem
[REMOTE_ADDR] => 127.0.0.1
[REMOTE_PORT] => 1071
[SCRIPT_FILENAME] => z:/home/localhost/www/myprimers/pr44.php
[SERVER_ADDR] => 127.0.0.1
[SERVER_ADMIN] => webmaster@localhost
[SERVER_NAME] => localhost
[SERVER_PORT] => 80
[SERVER_SIGNATURE] => <ADDRESS>Apache/1.3.33 Server at <A HREF="mailto:webmaster@localhost">localhost</A> Port 80</ADDRESS>
[SERVER_SOFTWARE] => Apache/1.3.33 (Win32) PHP/5.1.6
[SystemRoot] => C:\WINDOWS
[WINDIR] => C:\WINDOWS
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /MyPrimers/pr44.php
[SCRIPT_NAME] => /MyPrimers/pr44.php
[PATH_TRANSLATED] => z:/home/localhost/www/myprimers/pr44.php
[PHP_SELF] => /MyPrimers/pr44.php
[REQUEST_TIME] => 1196893515
[argv] => Array
(
)
[argc] => 0
)
$_ENV , ++. PHP .
$a + $b | $a $b. |
$a - $b | $a $b. |
$a * $b | $a $b. |
$a / $b | $a $b. |
$a % $b | $a $b. |
("/") , ( , ).
. , .
.
$a & $b | And | , $a, $b. |
$a | $b | Or | , $a $b. |
$a ^ $b | Xor | , $a $b, . |
~ $a | Not | , $a , . |
$a << $b | $a $b . | |
$a >> $b | $a $b . |
.
$a == $b | TRUE, $a $b. | |
$a === $b | TRUE, $a $b . | |
$a!= $b | TRUE, $a $b. | |
$a <> $b | TRUE, $a $b. | |
$a!== $b | TRUE, $a $b . | |
$a < $b | TRUE, $a $b. | |
$a > $b | TRUE, $a $b. | |
$a <= $b | TRUE, $a $b. | |
$a >= $b | TRUE, $a $b. |
?: (expr1)? (expr2): (expr3). expr2, expr1=TRUE, expr3, expr1= FALSE.
.
$a and $b | And | TRUE, $a, $b TRUE. |
$a or $b | Or | TRUE, $a $b TRUE. |
$a xor $b | Xor | TRUE, $a $b TRUE, . |
! $a | Not | TRUE, $a TRUE. |
$a && $b | And | TRUE, $a, $b TRUE. |
$a || $b | Or | TRUE, $a $b TRUE. |
and or , && || , && ||.
false 0, true 0. true 1. :
and or , && || , && ||.
false 0, true 0. true 1. :
<?php //pr7.php
$b=('2' & '9');
echo "b=", $b, "<br>";// '0'
$b=(2 && 9);
echo "b=", $b, "<br>";// '1'
$a=(3 | 9);
echo "a=", $a, "<br>";// 11
$c= $a and $b;
echo "c=", $c, "<br>";// '11'
echo "c=", $c=$a && $b,"<br>";// '1'
echo "c=", $c=3||4, "<br>";// '1'
echo "c=", $c=3 or 4, "<br>";//1
?>
+ , ( ). . .=.
<?php //pr4.php
$one = '1';
$two= "2";
echo $one, $two;
print $one+$two; // - 1 2 3
echo $one.$two; // - 12
$one = (int)$one; $two= (float)$two;
print $one+$two;//3
$a = "Hello ";
$a.= "World!"; // $a "Hello World!"
?>
:
$one $two , . : $one = (int)$one; $two= (float)$two;
. PHP , , , - . . , PHP , , 0. == !=, . :
<?php
$x=0;
$y=1;
if ($x == "") echo "<p>x - </p>";
if ($y == "") echo "<p>y - </p>";
// :
// x -
?>
, $x - . , ("") 0. , . , :
<?php
$x="string";
$y="string";
$z="";
if ($x === $z) echo "<p> X Z</p>";
if ($x === $y) echo "<p> X Y</p>";
if ($x!== $z) echo "<p> X Z</p>";
// :
// X Y
// X Z
?>
PHP C++: ifelse (elseif), switch, .
if :
if (boolexp) { /*, boolexp=true*/ } //1
else { /*, boolexp=false */ } //2
boolexp true, 1, 2. else , if ( 2), if. - if {} else if {}.
PHP, html .
<?php //pr8.php
$boolexp=true;
if ($boolexp):?> // php
<img src="img/A_A.jpg" >
<?php endif; // php
$c=5;
echo "c=", $c=$c or 4, "<br>";//1
?>
if() endif ( endwhile, endfor). PHP- HTML- , , :
// php
<img src="img/A_A.jpg" >
c=1<br>
:
<?php //pr81.php
$bexp=true;
if ($bexp) {?> // php
<img src="img/A_A.jpg" >
<?php } // php
$c=5;
echo "c=", $c=$c or 4, "<br>";//1
?>
PHP , ++:
:
while (boolexp) { /*, boolexp=true */ }
:
do { /*, boolexp=true */ }
while (boolexp);
for (exp1; boolexp; exp3){ /**/ } //
exp1 , boolexp , exp3 ( , ). , boolexp true.
E , :
foreach ($array as[$key=>] $value){ /**/ }
$key , value . $key .
:
<?php //pr9.php
$massiv = array('one','two','three');
foreach($massiv as $value)echo $value;//onetwothree
?>
.
<?
$a[]=1;
$a[]=2;
$a[]=4;
$a[]=8;
foreach ($a as $i) {$s=$s+$a[$i];}
echo ($s);
?>
: $s=6
, - .
C++ switch:
switch(exp) {
caseexp1:{ /**/ }
break;
caseexpN:{ /**/ }
break;
default: { /**/ }
}
exp , case, , break, exp1,, expN , C++.
break continue.
<html>
<head></head>
<body>
<p> </p>
<?php //pr10.php
$matrix = array();
$n = 3;
$m = 5;
for($i=0;$i<$n;$i++)
for($j=0;$j<$m;$j++){
$matrix[$i][$j] = rand(0,9);
}
for($i=0;$i<$n;$i++){
for($j=0;$j<$m;$j++){
echo($matrix[$i][$j]);
echo(" ");
}
echo("<br>");
}
$sum = array();
for($j=0;$j<$m;$j++){
$sum[$j] = 0;
for($i=0;$i<$n;$i++){
$sum[$j]+=$matrix[$i][$j];
}
}
echo("<br>");
for($i=0;$i<$m;$i++){
echo($sum[$i]);
echo(" ");
}
for($i=0;$i<$m-1;$i++)
for($j=0;$j<$m-1-$i;$j++){
if($sum[$j]>$sum[$j+1]){
$temp = $sum[$j];
$sum[$j] = $sum[$j+1];
$sum[$j+1] = $temp;
for($k=0;$k<$n;$k++){
$temp = $matrix[$k][$j];
$matrix[$k][$j] = $matrix[$k][$j+1];
$matrix[$k][$j+1] = $temp;
}
}
}
echo("<br>");
echo("new matrix:");
echo("<br>");
for($i=0;$i<$n;$i++){
for($j=0;$j<$m;$j++){
echo($matrix[$i][$j]);
echo(" ");
}
echo("<br>");
}
?>
</body>
</html>
:
function fname ($arg_1, $arg_2,..., $arg_n)
{
echo " \n";
return $val; //
}
return , .
<?php //pr11.php- , // m //
$m=rand(10,150); // m
print "m=$m<br>";
for($i=1;$i<$m;$i++)
{$k=f($i);
if($k!=0)print "$k<br>";
}
function f($n)
{$l=$n;
do
{
if(($l%10)==9)return $n;}
while(($l/=10)!=0);
return 0;
}
?>
. .
<html>
<head><title>Square</title></head>
<body>
:<br>
<?php //pr12
$a=rand(1,100);
$b=rand(1,100);
$c=rand(1,100);
print "$a<br>$b<br>$c<br>";
Proverka($a,$b,$c);
function Proverka($a,$b,$c)
{if(($a+$b<=$c)||($a+$c<=$b)||($b+$c<=$a))
echo " !!!!<br> !<br>";
else{
echo " ! !!!!<br>";
$s=Geron($a,$b,$c);
echo " <br> ";
print "$s";
}
}
function Geron($a,$b,$c){
$p=($a+$b+$c)/2;
return sqrt($p*($p-$a)*($p-$b)*($p-$c));
}
?>
</body>
</html>
list().
function numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = numbers();
& :
function &reference()
{
return $ref;
}
$newref =&reference();
<?
// x^n
function Evaluate($x,$n)
{
if ($n==1) return $x;
else return Evaluate($x,$n-1)*$x;
}
echo("x=5; n=3; res=");
echo(Evaluate(5,3));
echo("<BR>");
echo("x=0.2; n=3; res=");
echo(Evaluate(0.2,3));
?>
, . PHP , .
PHP, ++, . , &:
function f(&$str)
{
$str.= 'and extra.';
}
$str = 'This is a string, ';
f($str);
echo $str;// 'This is a string, and extra.'
$str, . $str $str .
C++ :
function f($ = "0")
{
return $.;
}
echo f(); // 0.
echo f("1");// 1.
, . , . .
, . , , . , , .. , .
$perem = 1; /* */
function Test ()
{
echo $perem;
}
Test ();
:
function Test ()
{
$perem = 1; /* */
echo $perem;
}
Test ();
, .
<?php //pr18.php
/* */
$a = 2;
$b = 3;
function Sum ()
{
global $a, $b; /* $a $b */
$c = $a + $b; /* */
echo $c;//5
global $myglobal;
$myglobal=0;
}
echo ($myglobal);// $myglobal
Sum ();
echo $myglobal; //0
?>
PHP $_GET[],$_POST[],$_REQUEST[],$_COOKIE[], $_FILES[],$_SERVER[],$_ENV[],$_SESSION[], PHP. PHP $GLOBALS.
, . . , . :
Function Count ()
{
static $counter = 0; /* $counter , */
echo $counter;
$counter++; }
Count(); //
()
. . :
$a = "hi";
. hi , , ..:
$$a="PHP"; /* $hi="PHP" */
print "$a ${$a}"; /* : hi PHP */
print "$a $hi"; /* , - */
. Web- PHP.net.
count() .
sort() .
shuffle() .
sizeof() count().
array_slice($array_name,offset, length) . $array_name , offset , , length , .
array_merge($array_name1, $array_name2) .
:
<?php //pr48
$massiv = array('
',
' , ',
' ',
' , ');
foreach($massiv as $value) echo $value;//
sort($massiv);//
foreach($massiv as $key=>$value)
echo ("<li>$key=>$value</li>");//
rsort($massiv);//
for($i=0;$i<count($massiv);$i++){
echo("<br>$massiv[$i]");
}
shuffle($massiv);//
for($i=0;$i<count($massiv);$i++){
echo("<br>$i-$massiv[$i]");
}
$massiv1=array_slice($massiv,2,3); // ,
// massiv1
print_r($massiv1);
?>
.
PHP . : strlen($str) , ltrim($str) , rtrim($str) , trim($str) - . :
<?php //str1.php
// - :<br />
$str=" .- ;
$str=ltrim($str); // <br />
echo "$str", strlen($str)," <br />";
$str=rtrim($str); // <br />
echo "$str"," <br />";
$str=" ". $str=trim($str);// <br />
echo "$str";
?>
:
- .- 89
- .-
- .-
strpos(string $where, string $what[,int $fromwhere =0]) $where $what . $fromwhere , $from, . , false.
string substr (string $str, int $start [, int $length]) - $str, $start $length. $length , $start $str. $start , $length , . $start , , $str.
string strstr (string $str, string $needl) - $str $needl. $str $needle . needl , FALSE. . stristr(). :
<?php //str2.php
echo strpos("Hello","el"),"<br/>"; // 1
echo strstr("Hello","el"),"<br/>";
//
// (===) (!==)
if (strpos("Norway","rwa")!== false)
echo " rwa Norway","<br/>";
// substr()
echo $rest = substr("abcdef", 1),"<br/>"; // "bcdef"
echo $rest = substr("abcdef", 0, 4),"<br/>";// "abcd"
$str = "Programmer";
echo substr($str,0,2),"<br/>"; // Pr
echo substr($str,-3,3),"<br/>"; // mer
// strstr()
$email = '[email protected]';
$domain = strstr($email, '@');
print "$domain, <br/>"; // @example.com
?>
:
Ello
rwa Norway
Bcdef
Abcd
Pr
Mer
@example.com,
strcmp(string $str1, string $str2)
: 0, ; -1, $str1 $str2; 1, , , $str1 "" $str2. strcmp() , strcasecmp(string $str1, string $str2) .
strspn() 1, , 2. strspn():
int strspn (string 1, string 2)
, strspn() . :
<? //Str13.php
$string1 = "butter";
$string2 = "Butter";
if ((strcmp($string1, $string2)) == 0)
print "Strings are equivalent!";
else
print "Strings are not equivalent!";
if ((strcasecmp($string1, $string2)) == 0)
print " !";
$password = "12345";
if (strspn($password, "1234567890") ==strlen($password))
print "Password cannot consist solely of numbers!";
?>
:
Strings are not equivalent!
!
Password cannot consist solely of numbers!
strcspn() 1, , 2. :
int strcspn (string 1, string 2);
str_replace(string $from, string $to, string $str)
$str $from ( ) $to . , , . , HTML <br>:
$st=str_replace("\n","<br>\n",$str)
, <br>\n , , . . .
string WordWrap(string $str, int $width=75, string $break="\n")
$str , $break, , $width . . , $break. , , , mail(). :
<?php //str3.php
$str = " , ";
// 20
$str = WordWrap ($str, 30, "<br>");
echo $str;
?>
: ,
string stripcslashes (string str) - . C- \n, \r.
strip_tags (string $str [, string $allowable_tags])
. $allowable_tags , . :
<? //str4.php
$str="<b> </b>";
echo "$str,<br/>";
$str=strip_tags($str);
echo " $str<br/>";
// , <b> <i>:
$str="<h1> </h1> <b><i> </i></b>";
echo "$str <br/>";
$str=strip_tags($str,"<b><i>");
echo "$str,<br/>";
$strip = strip_tags ($str); // -
echo "$strip<br/>";
?>
:
,
,
int ord ($str) - ASCII- $str, chr() ASCII :
<?php // str5.php
$str = "PHP";
echo $str[0]; // 'P'
$str="\n ";
if (ord($str) == 10)
print "The first character of \$str is a line feed.\n";
$str=chr(64); //# @
echo $str;
?>
: The first character of $str is a line feed. @
.
URL- .
UrlEncode(string $str)
URL- $str . :
echo "<a href=/script.php?param=".UrlEncode($UserData);
UrlDecode(string $st)
URL- . , , UrlEncode(), PHP .
HtmlSpecialChars(string $str)
, echo. - , .
( , "" "") HTML-, , " ". value , , , .
StripSlashes(string $str)
$str . : ", ', \ .
AddSlashes(string $str)
: ', " \. eval() ( , , , PHP-.
strpbrk(string, char) char false , ; strtoupper(string) ; strtolower(string) ; strrev(string) string . , PHP.
<?php//str6.php
$string = "Hello World";
$another_string = "Welcome to PHP";
echo strlen($string);
echo strtoupper($another_string);
echo strrev($another_string);
echo strpbrk($string, "W");
?>
:
WELCOME TO PHP
PHP ot emocleW
World
"Hello World", 11. "Welcome to PHP" . strrev . , strpbrk () "W". "World", .
PHP , , -.
string sprintf (string $format [, mixed args])
, $format. $format , %. . , $format.
<? // str7.php sprintf()
$num=5;
$location="tree";
$format = "There are %d monkeys in the %s";
printf($format,$num,$location); // : "There are 5 //monkeys in the tree".
//sprintf():
$year=3;
$month=10;
$day=11;
$dat = sprintf(" %03d-%03d-%02d ", $year, $month, $day);
echo $dat;
//sprintf():
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
echo $money;// will output "123.1";
$formatted = sprintf(" %01.2f", $money);
echo $formatted; // "123.10"
?>
:
There are 5 monkeys in the tree 003-010-11 123.1 123.10
printf(string $format [, mixed args,...]) , sprintf(), , .
PHP , - sscanf() fscanf(), .
mixed sscanf (string str, string format [, string var1]) - input- printf(). sscanf() str format. .
<? // str8.php sscanf()
$serial = sscanf("SN/23501","SN/%s",&$ser);
//
echo $serial,"<br>";
$mandate = "January 01 2011";
list($month, $day, $year) = sscanf($mandate,"%s %d %d");
echo "Item $ser was manufactured on: $year-".substr($month,0,3)."-$day\n";
// .
?>
:
Item 23501 was manufactured on: 2011-Jan-1
- : explode().
array explode (string , string $str [, int n]) . $str , n.
- string implode (string , array ) - . :
<? // str9.php
$info = "Minsk | baseball | indians";
$user = explode("|", $info);
//$user[0]="Minsk";$user[1]="baseball";$user[2]="Indians";
print_r($user);
$cities=array("Colum","Youngstown","Cleveland","Cincinnati");
$city_string = implode("l", $cities);
//$city_string="Colum1Youngstown1Cleveland1Cincinnati";
echo "<br>",$city_string;
?>
:
Array ([0] => Minsk [1] => baseball [2] => indians)
ColumlYoungstownlClevelandlCincinnati
string strtok (string $arg1, string $token) , arg1 (/tokens), $token. "This is an example string", e .
<? // str10.php
$string = "This is\tan example\nstring";
/* tab newline */
$tok = strtok($string," \n\t");
while ($tok) {
echo "Word=$tok<br>";
$tok = strtok(" \n\t")