Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Variable into a number

Posted on 2016-10-15
3
Medium Priority
?
87 Views
Last Modified: 2016-11-07
I'm having an issue with Less Than.  I am turning a variable into a number and evaluating it.
<?php
$percent = get_post_meta ($dt_grade_post_id, "marks-obtained-percent", true);
                                            $per = (int)$percent;
                                            if($per <= 89) {

								$user_option_alt = '<a href="'.get_permalink($quiz_id).'" class="dt-sc-button small">'.__('Retake Quiz', 'dt_themes').'</a>';
						
                                                       
                                                       } 

Open in new window



in my code $percent  = 90.  No matter what it always passes the valuation.  Wjhat am I doing wrong?
0
Comment
Question by:rgranlund
[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 35

Expert Comment

by:Terry Woods
ID: 41845324
Try adding some debugging and see what you get:
<?php
$percent = get_post_meta ($dt_grade_post_id, "marks-obtained-percent", true);
                                            $per = (int)$percent;
die("percent: '$percent', per: '$per'");  # add this line
                                            if($per <= 89) {

Open in new window

1
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41845402
I replicated your code here works as expected.

wp_postmeta record
ss76.jpgSource
<?php
define('WP_USE_THEMES', false);
require('../wp-blog-header.php');

$dt_grade_post_id = 999;
$percent = get_post_meta ($dt_grade_post_id, "marks-obtained-percent", true);
$per = (int)$percent;
$user_option_alt = '';
if($per <= 89) {
  $user_option_alt = '<a href="'.get_permalink($quiz_id).'" class="dt-sc-button small">'.__('Retake Quiz', 'dt_themes').'</a>';
} 
echo "Result: [{$user_option_alt}]<br/>";

Open in new window

Problem is somewhere else.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41845429
Var_export() is your friend.  So is the error_log().  You can use these functions together.  They will let you capture a "moment in time" representation of the contents of your variables, and write that information into a log file that you can inspect after the fact.  You might use something like this:
$xxx = var_export($percent, TRUE);
error_log($xxx);

Open in new window

But that said, one usual range of percentages is from 0 to 100, which translates to a two-digit decimal fraction expressing a value between 0.00 and 1.00.  When you cast these fractional values to integers, you may get unexpected results.
http://php.net/manual/en/language.types.integer.php

Here's how I would handle the issue...

1. Insert the data visualization code and check the error log to be sure you're working with the correct numeric values.
2. Run this little script to see how (int) casting works, then consider whether you want to use casting or the more mainstream approach of round().  Or if you're preparing a number for visual display, number_format() is often useful.
<?php // demo/temp_rgranlund.php
/**
 * https://www.experts-exchange.com/questions/28976679/PHP-Variable-into-a-number.html
 *
 * http://php.net/manual/en/language.types.integer.php
 */
error_reporting(E_ALL);

$questions = 100;
$correct   = 89.999;
$percent   = ($correct / $questions) * 100;
$integer   = (int)$percent;

echo PHP_EOL . "PERCENT $percent IS CAST TO INTEGER $integer";

Open in new window

Outputs:
PERCENT 89.999 IS CAST TO INTEGER 89

Open in new window

0

Featured Post

Technology Partners: 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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

722 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