I am trying to create a so-called Greek Numerals Converter tool. The input here will be letters and these letters will be converted into numbers. For every letter, there will be a corresponding number for it and also taking into consideration the digits. Now, my idea here is to create an array wherein all the letters and their corresponding numerical values will be put. So, when the user enters a combination of letters (up to 3 digits only), the convert() function will be looking for the corresponding values of those letters entered, get the values, and sum everything up. Unfortunately, as I went along the coding, I came up with this error (the one mentioned in the title) and I can't seem to figure out how to solve it. This might sound very stupid, but please do understand as I am still in the process of learning PHP.
Here are the codes that I was able to come up with.
<?php error_reporting(0);
class GreekNum
{
public function setText($text)
{
$this->text=$text;
}
public function getText()
{
return $this->text;
}
public function Convert($text)
{
$TextLength = strlen($text);
$text = strtoupper($text);
$text = str_split($text);
$collection = array(
"A" => 1,
"B" => 2,
"G" => 3,
"D" => 4,
"E" => 5,
"#" => 6,
"Z" => 7,
"Y" => 8,
"H" => 9,
"I" => 10,
"K" => 20,
"L" => 30,
"M" => 40,
"N" => 50,
"X" => 60,
"O" => 70,
"P" => 80,
"Q" => 90,
"R" => 100,
"S" => 200,
"T" => 300,
"U" => 400,
"F" => 500,
"C" => 600,
"$" => 700,
"W" => 800,
"3" => 900,
);
if(isset($text))
{
$total = 0;
for($i=0; $i<$TextLength; $i++)
{
if(array_key_exists($text[$i], $collection))
{
$total += $collection[$text[$i]];
}
if(end($text)!='.')
{
echo "Please make sure your line ends with a period. <br><br>";
return false;
}
}
return $total;
}
}
}
$GreekNum = new GreekNum();
$text = $_POST['text'];
$GreekNum->setText($text);
echo "<br>";
echo "<b>Entered Values: </b><br>". $GreekNum->getText();
echo "<br>";
echo "<br>";
echo "<b>Decimal Number Equivalence: </b><br/>". $GreekNum->Convert($text);
?>
The error is about the $total and I can't seem to figure out how to fix this. Any help would be very much appreciated. Thanks a lot!
This has been resolved already, but for the sake of whoever might need this, I'm editing this again to the code that currently works for me.
You need to declare $total
before you can use it, try the following
....
$total = 0;
for($i=0; $i<$TextLength; $i++)
{
.....
The reason it doesn't work in your case is because you are doing $total += $text[$i];
this expands to $total = $total + $text[$i];
unfortunately the $total
on the right hand side of the equals does not exist so it is undefined.
Im also wondering why you are accessing $text as if it were an array, its a String, I believe $collection is the array with your values in.
So what you need to do is split the $text
string first into an array of its characters. Then for each of these characters look it up in the collection, if it exists add its value to the running total.
....
$text = "AQ3";
$TextLength = strlen($text);
$splitText = str_split($text);
$total = 0;
for($i=0; $i<$TextLength; $i++)
{
$currentCharacter = $splitText[$i];
if(array_key_exists($currentCharacter, $collection))
{
$total += $collection[$currentCharacter];
}
}
....
As you can see $splitText[$i]
is the current character, this is used as a key in the search but is also needed in the following statement $total += $collection[$splitText[$i]];
as it is the key used to lookup.
For the string AQ3 this will return the value 991 which I presume was your intended result?
Because you never defined $total
before you referenced it in the for
loop. You should define it like this (you probably want to start it at zero):
$total = 0; // <-- Define total here
for($i=0; $i<$TextLength; $i++)
{
if(array_key_exists($text[$i], $collection))
{
$total += $text[$i];
}
}
return $total;
There error is on this line I assume:
$total += $text[$i];
The problem is that you are in essence trying to do this:
$total = $total + $text[$i];
On your first iteration the value of $total is not set, so you get this PHP notice.
You might want to declare $total = 0 or similar at some earlier point in the script.
basically you declared $total inside the for loop. the reason this is an issue is because when you declare a variable somewhere, that means it can only be used in that domain. think of you entire file as a circle and every time you create a for loop, it creates little circles inside it. so when you created $total, it was created in that little circle inside the bigger circle. its scope is only within the for loop. when you exit the for loop, the variable is gone. some languages like python allow you to do this, but perhaps not php. if you want to fix this, declare you $total variable outside the for loop. this means anything in the bigger circle can use it.