cant get string into a variable [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
so i am pretty new to OOP PHP.
i am trying to get a feel for it,
i am makeing a simple class with 2 functions
one for speed of the car(he try's te see if the speed is a good value)
and one for color of the car(check if the color is vailid)
the error i got:
Catchable fatal error: Object of class Auto could not be converted to string in E:\Site peter+usbweb\root\stage\oef\Auto.php on line 44
here is my code:
<?php
class Auto {
public $mKleur = "";
public $mSpeed = "";
public function speed ($var){
if(is_numeric($var)){
if($var < 0 OR $var == 0)
{
$this->mSpeed = ("$var kmpu rijd die waggie niet eens?!");
}
elseif ($var > 206 OR $var == 206)
{
$$this->mSpeed = ("$var kmpu haalt de auto niet!");
}
elseif($var > 0 AND $var < 206)
{
$$this->mSpeed = ("de auto reed met $var kmpu langs!");
}
}
else
{
$$this->mSpeed = ( " $var dat niet eens een getal....");
}
}
private $sKleur = "";
public function __set( $sAttribuut, $sValue ){
switch( $sAttribuut ){
case 'sKleur':
$aValideKleuren = array('groen', 'rood', 'blauw', 'paars', 'geel', 'wit', 'zwart','grijs','oranje' );
if( !in_array( strtolower( $sValue ), $aValideKleuren ) ){
$$this->mKleur = ("Kleur niet bekend: $sValue");
}
else{
$$this->mKleur = ("kleur is bekent namelijk: $sValue");
}
$this->sKleur = $sValue;
break;
default:
break;
}
}
public function __get( $sAttribuut ){
if( isset( $this->$sAttribuut ) ){
return $this->$sAttribuut;
}
}
public function kk()
{
$message = array('gSpeed' => $this->mSpeed, 'gKleur' => $this->mKleur);
return($message);
}
}
?>
it does work if i use echo but i cant get it te return the value's
anyone know why?

You have a mistake. You put double $ before "this". Change $$this to $this

Related

If statement inside an if statement dilemma [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following code, but I have no idea what I'm doing wrong. Any idea how to fix this?
<?php
if($testing = series_get_meta('code_name')) {
if ($testing == 'Pre1';) {
echo "Pre1";
} elseif ($testing == 'Gem2';) {
echo "Gem2";
} elseif ($testing == 'Remi1';) {
echo "Remi1";
} else {
echo "Default";
}
}
?>
You have a couple typos.
Don't use semi colons in the () of your if/else statements.
You have an extra parenthesis at the end of your last else statement.
Like so:
if ($testing == 'Pre1')
{
echo "Pre1";
} elseif ($testing == 'Gem2') {
echo "Gem2";
} elseif ($testing == 'Remi1') {
echo "Remi1";
} else {
echo "Default";
}
However you task is probably better suited to using a switch() statement like so:
switch ($testing) {
case 'Pre1':
echo 'Pre1';
break;
case 'Gem2':
echo 'Gem2';
break;
case 'Remi1':
echo 'Remi1';
break;
default:
echo 'Default';
}
Hey you have added semicolon inside the if condition, just remove that
<empty>

error with $_POST [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Well, I have the problem narrowed down to one group of code, and here it is:
<?php
if( isset($_POST['submit'])){
**if($_POST['shortsitename'] == ""){$newshortsitename = $shortsitename;}else{ $newshortsitename = $_POST['shortsitename'];}
if($_POST['organization'] == ""){$neworganization = $organization;}else{$neworganization = $_POST['organization'];}
if($_POST['city'] == ""){$newcity = $city;}else{$newcity = $_POST['city'];}
if($_POST['state'] == ""){$newstate = $state;}else{$newstate = $_POST['state'];}**
$user_lvl = \Fr\LS::getUser("user_lvl");
$updateGroup = \Fr\LS::updateGroupinfo(
array(
"shortsitename" => $newshortsitename,
"organization" => $neworganization,
"city" => $newcity,
"state" => $newstate
), $user_lvl
);
if($updateGroup === "success"){
echo "<label>Success.</label>";
}elseif($updateGroup === false){
echo "<label>Update failed.";
}
?>
Basicly, I have a form that lets user change the information about their organization in a database. However, when I have this code in the site, it returns internal server error 500. When I removed this block of code, the site displays fine. My main question is, is the code that is bolded correct, or did I miss word it?
**Edit: It's not letting me bold the code, but there are "stars" around the code I am talking about.
Your Update failed <label> tag does not have a closing </label> and I think you are missing 2 } on the end. Is $shortsitename defined above?
<?php
if( isset($_POST['submit'])){
//**
if($_POST['shortsitename'] == ""){
$newshortsitename = $shortsitename;
}else{
$newshortsitename = $_POST['shortsitename'];
}
if($_POST['organization'] == ""){
$neworganization = $organization;
}else{
$neworganization = $_POST['organization'];
}
if($_POST['city'] == ""){
$newcity = $city;
}else{
$newcity = $_POST['city'];
}
if($_POST['state'] == ""){
$newstate = $state;}else{
$newstate = $_POST['state'];
}
//**
$user_lvl = \Fr\LS::getUser("user_lvl");
$updateGroup = \Fr\LS::updateGroupinfo(
array(
"shortsitename" => $newshortsitename,
"organization" => $neworganization,
"city" => $newcity,
"state" => $newstate
), $user_lvl
);
if($updateGroup === "success"){
echo "<label>Success.</label>";
}elseif($updateGroup === false){
echo "<label>Update failed.";
}
?>
Above is reformatted version of your code.

why is php class class giving me error when trying to access the class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this is the error I get.
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
in /home/students/000313753/public_html/10065/php/lab2/lab2.php on
line 121
my class
class validate
{
public $errorArray;
function userName($name)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($name))
{
if(strlen($name) <= 5)
{
$this->$errorArray['name'] = 'The username must be 5 characters.';
}
}
else
{
$this->$errorArray['name'] = 'Username cannot be empty';
}
}
}
function validateEmail($email)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$this->$errorArray['email'] = 'Wrong email format.';
}
}
}
function validateYear($year)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($year))
{
if ($year < 1000 || $year > 2100)
{
$this->$errorArray['year'] = 'Year must be a 4 digit number.';
}
}
else
{
$this->$errorArray['year'] = 'Year cannot be empty';
}
}
function validateProvinces($provinces)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($provinces))
{
if (empty($provinces))
{
$this->$errorArray['provinces'] = 'Please select one or more provinces';
}
}
}
}
function validateStatus($status)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($status))
{
if (empty($status))
{
$this->$errorArray['status'] = 'Please select one or more items from status';
}
}
}
}
}
//access class
$validation = new validate()
$validation->userName($_POST["name"]);
$validation->validateYear($_POST["year"]);
$validation->validateProvinces($_POST["provinces"]);
$validation->validateStatus($_POST["status"]);
$validation->validateEmail($_POST["email"]);
?>
I am a beginner and I have over a day trying to debug. Please do not just fix my code provide me with an explanation of why my code do not work.
Thank you in advance for any help.
You're missing a closing semi-colon here:
$validation = new validate()
Should be:
$validation = new validate();
you are missing closing brace } in function validateYear()

why isnt this equal function working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
im trying to make something to change password.
nkode is the new password
gkode is the old password
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST[nkode]);
$gkode = md5($_POST[gkode]);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == '$nukode'){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}
both $gkode and $nukode outputs the 100% same, yet i get the fail error... whats wrong?
if($gkode == '$nukode'){
Look at those quotes. You're comparing the contents of $gkode against a string which has the characters $, n, u, etc... in it...
Maybe you wanted
if($gkode == $nukode){
instead?
remove quotes from $nukode variable...
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST['nkode']);
$gkode = md5($_POST['gkode']);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == $nukode){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}

How many variable checks should you do? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Something I've never been sure about is how many variable checks to do in PHP. For example take the following piece of code. I am not checking any of the variables before I assign them or pass them to a function to see if they contain what I expect
$carId = '12';
$aCar = fetchCar($carId);
$make = $aCar['make'];
$model = $aCar['model'];
$yearMade = $aCar['year'];
$age = calcAge($yearMade);
Now if I add some checks
$carId = '12';
if(is_numeric($carId))
{
$aCar = fetchCar($carId);
if(isset($aCar['make']) && is_string($aCar['make']))
{
$make = $aCar['make'];
}
else
{
//Report error
}
if(isset($aCar['model']) && is_string($aCar['model']))
{
$model = $aCar['model'];
}
else
{
//Report error
}
if(isset($aCar['year']) && is_numeric($aCar['year']))
{
$yearMade = $aCar['year'];
$age = calcAge($yearMade);
}
else
{
//Report error
}
}
else
{
//Report errors
}
The code is now better but is it a bit too excessive and bloated? Should I be doing this many checks?
If I shouldn't be doing this many checks where do you draw the line between what you should and shouldn't check?
This is the dilemma of a dynamic type language.
It depends heavily on what fetchCar() function is doing.
The approach i would take is assume fetchCar is returning a car array or throwing exception.
If you combine this with good exception handling logic you can end up with clean and stable code.
For example:
function fetchCar($id) {
$car = queryDatabaseSomehow();
if (empty($car)) {
throw new ExceptionNotFound();
}
//eventually you can put your type checking here?
if (!isset($car['x']) || !is_string($car['x'])) {
throw new ExceptionDb();
}
}
echo fetchCar(3)['make'];
Also if you would like to do this super-proper and go fully OOP, Car should become a class with make,model and year as its members. fetchCar() would return Car or throw Exception. But this is not always desirable of course.
One issue that some people haven't noticed. Be wary of using is_string:
<?php
$var = "test";
$var['something'] = 2;
if(is_string($var['something'])) {
echo "Hello world!"; // Will echo this because $var is a string!
} else {
echo "Hello hell!";
}
echo "<br/>";
echo $var['something']; // returns 2
?>
PHPFiddle.
Compare it with this:
$var = array('something' => 2);
if(is_string($var['something'])) {
echo "Hello world!"; // $var is now an array
} else if (is_numeric($var['something'])) {
echo "Hello hell!"; // Will echo this because $var is string!
}
echo "<br/>";
echo $var['something'];
You need to check whether $var is an array, as it might give you unexpected results. isset($var['something']) will return true in the first example.
To answer your question, I don't think those are too many checks. It really depends on what fetchCar() does and how it gets the data. If you can't trust it (say, it's based on user data) then you should perform all these checks. If not, then there is no point really.
I rather turn it all into a function that can be reused for these cases.
function check_keys($arr_check, $arr_cond) {
$boo_success = TRUE;
foreach(array_keys($arr_cond) as $h)
if (in_array($arr_cond[$h], array('is_string', 'is_numeric'))) {
if ( ! isset($arr_check[$h]) or ! ($arr_cond[$h]($arr_check[$h]))) {
$boo_success = FALSE;
echo "The key {$h} is missing!";
// If run through a class, $this->errors[] = 'error message';
}
} else {
$boo_success = FALSE;
echo 'Invalid function';
}
return $boo_success;
}
$arr_keys = array('make' => 'is_string',
'model' => 'is_string',
'year' => 'is_numeric');
if (check_keys($aCar, $arr_keys)) {
// Run successful stuff
}

Categories