Solved

Php sprintf('%u', $x) - explanation

Posted on 2013-01-11
3
444 Views
Last Modified: 2013-01-11
Hello experts.
I'm using coldfusion server language and need help to understand this function:
// IP ADDRESSES ARE UNSIGNED AND MAY RETURN NEGATIVE VALUES
$ip_number = sprintf('%u', $x);

(4 step - second code - line 39 here: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_3437-IP-Address-to-Country-in-PHP.html)

Any help?
0
Comment
Question by:Panos
3 Comments
 
LVL 30

Assisted Solution

by:Brad Howe
Brad Howe earned 150 total points
ID: 38766506
Just formating hte variable ip_number to an integer.

http://php.net/manual/en/function.sprintf.php

eg: string sprintf ( string $format [, mixed $args [, mixed $... ]] )

%u, u - the argument is treated as an integer, and presented as an unsigned decimal number.
$X is a variable placeholder within the script.

// IF NO IP ADDRESS IS GIVEN, USE THE REMOTE ADDRESS OF THE CLIENT
$ip = $_SERVER["REMOTE_ADDR"];
// CONVERT TO BINARY - SEE http://php.net/manual/en/function.ip2long.php
$x = ip2long($ip);
// IP ADDRESSES ARE UNSIGNED AND MAY RETURN NEGATIVE VALUES
$ip_number = sprintf('%u', $x);


Cheers,
Hades666
0
 
LVL 10

Accepted Solution

by:
d4durvesh earned 350 total points
ID: 38766600
Hi panosms,

As you may know we use 'echo' keyword to display output on the page,but sometimes it becomes tedious and bit of dirty.Lets see an example,
$name = "Gates";
$age = 55;
$work = "Microsoft"
echo "My name is $name and my age is $age,I work in $work";

Open in new window

As you see the string becomes to much of lengthy and unreadable.
So to make life simpler there is one way,

Use function printf() [hold on I will come to sprintf() ]
 
printf() functions works in following way
It takes two arguments,string to be displayed and variables to be inserted in it,
Example,
$name = "Gates";
$age = 55;
$work = "Microsoft"

printf("My name is %s and my age is %d,I work in %s",$name,$age,$work);

Output: My name is Gates and my age is 55,I work in Microsoft

Open in new window

That means it adds string in place of %s i.e., variable $name,number in place of %d.

But suppose we want to store this output we will try to do like this,
$name = "Gates";
$age = 55;
$work = "Microsoft"

$output = printf("My name is %s and my age is %d,I work in %s",$name,$age,$work);
echo $output;

Open in new window

But hold on this is not going to work the output we will get is
My name is Gates and my age is 55,I work in Microsoft45
The 45 is number of characters are there in string.
So to avoid this we do slightly other way,
we use sprintf() function.
The only difference between sprintf() and printf() is that sprintf() stores the value in variable.

$name = "Gates";
$age = 55;
$work = "Microsoft"

$output = sprintf("My name is %s and my age is %d,I work in %s",$name,$age,$work);
echo $output;

Open in new window

Here are some things you need to remeber,
%s reprensent string,
%d represent signed decimal
%u represent integer and presented as an unsigned decimal number.

There are more arguments you can pass on just check this,
http://php.net/sprintf

Also let me know if you still having doubts,I will be glad to explain you in more detail.

Thank You,
Naik Durvesh.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38766724
Thank you very much for your help.
regards
panos
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question