Hello im trying to make an object as learning task which takes a number and calculate it three times and echos every step.
<?
// neues Objekt der Klasse erzeugen
$stringManager = new StringManager();
// output String initialisieren
$meinString = "";
$zahl = "100";
$stringManager->setMeinString($meinString);
$stringManager->setZahl($zahl);
// Schritte ausführen
for ($i=1; $i<=3; $i++) {
$stringManager->machSchritt($i,$zahl);
}
// String ausgeben
echo $stringManager->getMeinString();
// Klasse StringManager
class StringManager {
var $meinString;
var $zahl;
function StringManager() {
}
function machSchritt($welchenSchritt,$zahl) {
switch ($welchenSchritt) {
case 1:
$zahl + 50;
break;
case 2:
$zahl / 2;
break;
case 3:
$zahl * 5;
break;
default:
break;
}
$this->append("schritt".$welchenSchritt." fertig...");
$this->append("Zahl:".$zahl." ");
}
function append($what) {
$this->meinString .= $what;
}
//function append($what) {
//$this->zahl .= $what;
//}
function setMeinString($value) {
$this->meinString = $value;
}
function getMeinString() {
return $this->meinString;
}
function setZahl($value) {
$this->zahl = $value;
}
function getZahl() {
return $this->zahl;
}
}
?>
My output is: "schritt1 fertig...Zahl:100 schritt2 fertig...Zahl:100 schritt3 fertig...Zahl:100"
But I expect it to be "schritt1 fertig...Zahl:150 schritt2 fertig...Zahl:75 schritt3 fertig...Zahl:375
Please help me find what im doing wrong.
your problem is between this lines:
$zahl + 50;
break;
case 2:
$zahl / 2;
break;
case 3:
$zahl * 5;
if you want to increase/manipulate the variable $zahl so you have to use it:
$zahl += 50;
break;
case 2:
$zahl /= 2;
break;
case 3:
$zahl *= 5;
btw: your code is deprecated.
class StringManager {
var $meinString;
var $zahl;
function StringManager() {
}
}
should replace with
class StringManager {
private $meinString;
private $zahl;
public function __construct() {
}
}
update:
ah, now i understand, you need to work with your value.
so use this instead:
$this->zahl += 50;
break;
case 2:
$this->zahl /= 2;
break;
case 3:
$this->zahl *= 5;
$this->append("Zahl:".$this->zahl." ");
But it would be better, to use your setter (with typecasting) and getter methods.
warning
your code looks like php4 code. php4 is not supported since 2008 (http://php.net/eol.php).
Related
I have checked the PHP source code and can not find the "require" function.
I only found a piece code relatives to it on main/main.c from line 976
switch (EG(current_execute_data)->opline->extended_value) {
case ZEND_EVAL:
function = "eval";
is_function = 1;
break;
case ZEND_INCLUDE:
function = "include";
is_function = 1;
break;
case ZEND_INCLUDE_ONCE:
function = "include_once";
is_function = 1;
break;
case ZEND_REQUIRE:
function = "require";
is_function = 1;
break;
case ZEND_REQUIRE_ONCE:
function = "require_once";
is_function = 1;
break;
default:
function = "Unknown";
}
Do you know which file defines the "function"?
Hy,
I got switch case inside function when but when i call it, i got error Undefined Variable and i don't know why (i use PHP 8)
private function getIDFromBrand() {
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} else {
if ($this->typecoke== 1) {
$id = 3;
}
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
}
return $id; // Error Undefined variable $id
}
When i declare $id at the top of my function, like
$id = null
or
$id = 0
The switch doesn't update it, so it will return null or 0, it will return the declared value.
Your switch statement has no default branch, so if $this->brand is, say, "Stack Overflow", it will not run any of the statements, and $id will never be set.
See the PHP manual for the switch statement:
A special case is the default case. This case matches anything that wasn't matched by the other cases.
Similarly, if $this->brand is "Coke", but $this->typecoke is, say, 42, it will not match either of the conditions in that branch.
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} elseif ($this->typecoke== 1) {
$id = 3;
} else {
$id = -1; // WAS PREVIOUSLY NOT SET
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
default:
$id = -1; // WAS PREVIOUSLY NOT SET
break;
}
This code is supposed to output something like:
You are, at this moment, living in the 11th second of the 2nd minute of the 3rd hour of the 9th day of the 5th month of the 2017th year since the begining of the International calender.
Instead it outputs this: https://prnt.sc/fli92p
Have no idea what the problem is.
date_default_timezone_set(//location...);
say_time();
function say_time()
{
$o = ' of the';
class time_value
{
public $t, $name, $display;
protected $n, $suf;
function __construct()
{
$this->display = " $this->n"."$this->suf"." $this->name";
$this->n = date($this->t,time());
switch ($this->n)
{
case 1:
$suf = 'st';
break;
case 2:
$suf = 'nd';
break;
case 3:
$suf = 'rd';
break;
default:
$suf = 'nth';
break;
}
}
}
$sec = new time_value;
$sec->t = 's';
$sec->name = 'seconds';
$min = new time_value;
$min->t = 'i';
$min->name = 'minutes';
$hr = new time_value;
$hr->t = 'G';
$hr->name = 'hours';
$day = new time_value;
$day->t = 'j';
$day->name = 'days';
$mon = new time_value;
$mon->t = 'n';
$mon->name = 'months';
$yr = new time_value;
$yr->t = 'Y';
$yr->name = 'years';
echo "You are, at this moment, living in the "
.$sec->display .$o
.$min->display .$o
.$hr ->display .$o
.$day->display .$o
.$mon->display .$o
.$yr ->display .
" since the begining of the International calender.";
echo $sec->display;
}
The line:
$this->display = " $this->n"."$this->suf"." $this->name";
is the first line of the class' constructor. It stores in the $display property of the object a string that contains only spaces because the values it contains are not set yet.
Read about double-quotes strings and variables parsing inside double-quotes strings.
In order to work, the class time_value should be like this:
class time_value
{
private $t, $name, $display;
public function __construct($t, $name)
{
$this->t = $t;
$this->name = $name;
$n = date($this->t, time());
switch ($n)
{
case 1:
$suf = 'st';
break;
case 2:
$suf = 'nd';
break;
case 3:
$suf = 'rd';
break;
default:
$suf = 'th';
break;
}
$this->display = " {$n}{$suf} {$this->name}";
}
public function display() { return $this->display; }
}
$sec = new time_value('s', 'seconds');
$min = new time_value('i', 'minutes');
// all the other time components here...
echo $sec->display().$o.$min->display(); // ...
The next step toward object-oriented programming is to encapsulate the generation of all time components into the time_value class (or into another class that uses time_value instances, if you like it more) and have the code of function say_time() look like this:
function say_time()
{
$time = new time_value();
echo "You are, at this moment, living in the ".$time->display("of the")." since the begining of the International calender.";
}
Before I save my model, I would like to check if $ CONT_CEDULA meets the requirements. If not, then don't save. But when saving, it is as if the variable $ CONT_CEDULA hasn't got any data. I want to know if I'm doing well or need some other event or function. Also, the echo outputs no data.
beforeSave method
public function beforeSave() {
echo $this->$CONT_CEDULA;
switch (strlen($this->$CONT_CEDULA)) {
case 10:
return validarCI($this->$CONT_CEDULA);
break;
case 13:
return validarRUC($this->$CONT_CEDULA);
break;
default:
echo "Numero de caracteres invalidos" ;
return FALSE;
}
SpmContacto model
<?php
class SpmContacto extends \Phalcon\Mvc\Model {
public $CONT_CODIGO;
public $CONT_CEDULA;
public $CONT_RUCIDE;
public $CONT_NOMBRE;
public $CON_ESTADO;
public $CONT_TELEFO;
public $CONT_DIRECC;
public $CONT_AREA;
public $CONT_CARGO;
public $CONT_TIPOXX;
public $CONT_EMAIL;
public $CONT_USUARIO;
public $CONT_CLAVE;
public $CONT_CLAVEE;
public $CONT_FECNACI;
public $CONT_FECINSC;
public $CONT_TIPOCODIGO;
/**
* Initialize method for model.
*/
public function initialize() {
$this->setSchema("SPOLS");
$this->hasMany('CONT_CODIGO', 'SPMREFERENCIA', 'CONT_CODIGO', array('alias' => 'SPMREFERENCIA'));
$this->hasMany('CONT_CODIGO', 'SPTDETALLE', 'CONT_CODIGO', array('alias' => 'SPTDETALLE'));
$this->hasMany('CONT_CODIGO', 'SPTENCABEZADO', 'CONT_CODIGO', array('alias' => 'SPTENCABEZADO'));
}
function validarCI($strCedula) {
$suma = 0;
$strOriginal = $strCedula;
$intProvincia = substr($strCedula, 0, 2);
$intTercero = $strCedula[2];
$intUltimo = $strCedula[9];
if (!settype($strCedula, "float"))
return FALSE;
if ((int) $intProvincia < 1 || (int) $intProvincia > 23)
return FALSE;
if ((int) $intTercero == 7 || (int) $intTercero == 8)
return FALSE;
for ($indice = 0; $indice < 9; $indice++) {
//echo $strOriginal[$indice],'</br>';
switch ($indice) {
case 0:
case 2:
case 4:
case 6:
case 8:
$arrProducto[$indice] = $strOriginal[$indice] * 2;
if ($arrProducto[$indice] >= 10)
$arrProducto[$indice] -= 9;
//echo $arrProducto[$indice],'</br>';
break;
case 1:
case 3:
case 5:
case 7:
$arrProducto[$indice] = $strOriginal[$indice] * 1;
if ($arrProducto[$indice] >= 10)
$arrProducto[$indice] -= 9;
//echo $arrProducto[$indice],'</br>';
break;
}
}
foreach ($arrProducto as $indice => $producto)
$suma += $producto;
$residuo = $suma % 10;
$intVerificador = $residuo == 0 ? 0 : 10 - $residuo;
return ($intVerificador == $intUltimo ? TRUE : FALSE);
}
function validarRUC($strRUC) {
if (strlen($strRUC) != 13)
return FALSE;
$suma = 0;
$strOriginal = $strRUC;
$intProvincia = substr($strRUC, 0, 2);
$intTercero = $strRUC[2];
if (!settype($strRUC, "float"))
return FALSE;
if ((int) $intProvincia < 1 || (int) $intProvincia > 23)
return FALSE;
if ((int) $intTercero != 6 && (int) $intTercero != 9) {
if (substr($strRUC, 10, 3) == '001')
return validarCI(substr($strRUC, 0, 10));
return FALSE;
}
if ((int) $intTercero == 6) {
$intUltimo = $strOriginal[8];
for ($indice = 0; $indice < 9; $indice++) {
//echo $strOriginal[$indice],'</br>';
switch ($indice) {
case 0:
$arrProducto[$indice] = $strOriginal[$indice] * 3;
break;
case 1:
$arrProducto[$indice] = $strOriginal[$indice] * 2;
break;
case 2:
$arrProducto[$indice] = $strOriginal[$indice] * 7;
break;
case 3:
$arrProducto[$indice] = $strOriginal[$indice] * 6;
break;
case 4:
$arrProducto[$indice] = $strOriginal[$indice] * 5;
break;
case 5:
$arrProducto[$indice] = $strOriginal[$indice] * 4;
break;
case 6:
$arrProducto[$indice] = $strOriginal[$indice] * 3;
break;
case 7:
$arrProducto[$indice] = $strOriginal[$indice] * 2;
break;
case 8:
$arrProducto[$indice] = 0;
break;
}
}
} else {
$intUltimo = $strOriginal[9];
for ($indice = 0; $indice < 9; $indice++) {
//echo $strOriginal[$indice],'</br>';
switch ($indice) {
case 0:
$arrProducto[$indice] = $strOriginal[$indice] * 4;
break;
case 1:
$arrProducto[$indice] = $strOriginal[$indice] * 3;
break;
case 2:
$arrProducto[$indice] = $strOriginal[$indice] * 2;
break;
case 3:
$arrProducto[$indice] = $strOriginal[$indice] * 7;
break;
case 4:
$arrProducto[$indice] = $strOriginal[$indice] * 6;
break;
case 5:
$arrProducto[$indice] = $strOriginal[$indice] * 5;
break;
case 6:
$arrProducto[$indice] = $strOriginal[$indice] * 4;
break;
case 7:
$arrProducto[$indice] = $strOriginal[$indice] * 3;
break;
case 8:
$arrProducto[$indice] = $strOriginal[$indice] * 2;
break;
}
}
}
foreach ($arrProducto as $indice => $producto)
$suma += $producto;
$residuo = $suma % 11;
$intVerificador = $residuo == 0 ? 0 : 11 - $residuo;
//echo "$intVerificador == $intUltimo";
return ($intVerificador == $intUltimo ? TRUE : FALSE);
}
function validarID($strId) {
switch (strlen($strId)) {
case 10:
return validarCI($strId);
break;
case 13:
return validarRUC($strId);
break;
default:
return FALSE;
}
}
public function beforeSave() {
echo $this->$CONT_CEDULA;
switch (strlen($this->$CONT_CEDULA)) {
case 10:
return validarCI($this->$CONT_CEDULA);
break;
case 13:
return validarRUC($this->$CONT_CEDULA);
break;
default:
echo "Numero de caracteres invalidos";
return FALSE;
}
//echo $op;
}
public function getSource() {
return 'SPM_CONTACTO';
}
public static function find($parameters = null) {
return parent::find($parameters);
}
public static function findFirst($parameters = null) {
return parent::findFirst($parameters);
}
By default Phalcon wont output any data you "echo" in your controllers or your models. The easy 'workaround' is to die('your output');
If you return a value in a switch, it is unnecessary to add a break; to the end of your case.
public function beforeSave()
{
// check the content of $this->$CONT_CEDULA
var_dump($this->$CONT_CEDULA);
die;
switch (strlen($this->$CONT_CEDULA)) {
case 10:
return validarCI($this->$CONT_CEDULA);
case 13:
return validarRUC($this->$CONT_CEDULA);
default:
// an example to test if your code enters this case.
die("Numero de caracteres invalidos");
return FALSE;
}
}
I am trying to avoid duplicating my code by checking the variable if it is a certain operator.
Basically..
$op = $_POST['operator'];
$x = 5;
$y = 2;
$result = $x /* $op instead of '+'/'-'/'*'/'/'/'%' */ $y;
Is this possible or will I have to send the operator as a String and duplicate the code per operator type?
It's a lot safer to do something like this:
$x = 5;
$y = 2;
switch($_POST['operator']){
case '+':
$result = $x + $y;
break;
case '-':
$result = $x - $y;
break;
case '*':
$result = $x*$y;
break;
case '/':
$result = $x/$y;
break;
case '%':
$result = $x % $y;
break;
default:
$result = 'Operator not supported';
}
Something along those lines.
Ahem. You can eval.
$result = eval("$x $op $y");
But this is DANGEROUS and you should sanitize your variables with great care. There is a saying that goes something like "If your problem requires use of eval, then the problem is wrong." Something like that. It's almost certainly preferable to do something like this:
function apply_op($x, $y, $op) {
switch ($op) {
case '+': return $x + $y;
...
}
}
you can make this:
$operators = array("+", "-","*","%","/");
$op = $_POST["operator"];
if(in_array($op, $operators)) {
echo eval("$x $op $y");
} else {
echo "Operator not supported";
}