Solved

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

Posted on 2013-01-11
3
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination 1 44
Link failure 16 64
Length of for loop to be dynamic 2 33
Change a button after e-mail 4 20
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 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